title: Spring MVC login functionality not working. Always redirecting to login page
body: <p>I was trying Spring MVC login functionality in one of my sample application. But, it always redirects to login page.</p>
  
  <p>Below is my code:
  <strong>application-security.xml</strong></p>
  
  <pre class="lang-html prettyprint-override"><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans:beans xmlns="http://www.springframework.org/schema/security"
      xmlns:beans="http://www.springframework.org/schema/beans" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/security  http://www.springframework.org/schema/security/spring-security-3.2.xsd"&gt;
  
      &lt;http auto-config="true"  use-expressions="true"&gt;
          &lt;intercept-url pattern="/admin/login" access="permitAll" /&gt;
          &lt;intercept-url pattern="/admin/**" access="hasRole('ROLE_USER')" /&gt;
          &lt;form-login login-page="/admin/login" default-target-url="/admin/student" authentication-failure-url="/admin/login/failed" /&gt;
          &lt;logout logout-success-url="/" /&gt;
      &lt;/http&gt;
  
      &lt;authentication-manager alias="authenticationManager"&gt;
          &lt;authentication-provider&gt;
              &lt;user-service&gt;
                  &lt;user name="roul@gmail.com" password="passw0rd" authorities="ROLE_USER" /&gt;
              &lt;/user-service&gt;
          &lt;/authentication-provider&gt;
      &lt;/authentication-manager&gt;
  
  &lt;/beans:beans&gt;
  </code></pre>
  
  <p><strong>AuthenticationController.java</strong></p>
  
  <pre class="lang-html prettyprint-override"><code>package com.spring.schoolmangement;
  
  import java.util.Locale;
  
  import org.springframework.stereotype.Controller;
  import org.springframework.ui.Model;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestMethod;
  
  /**
   * Handles requests for for user authentication.
   */
  @Controller
  public class AuthenticationController {
  
      /**
       * Simply selects the home view to render by returning its name.
       */
      @RequestMapping(value = {"/admin/login", "/admin/login/"}, method = RequestMethod.GET)
      public String displayLogin(Locale locale, Model model) {
          return "login-form";
      }
  
      @RequestMapping(value = {"/admin/login/failed", "/admin/login/failed/"}, method = RequestMethod.GET)
      public String validateLogin(Locale locale, Model model) {
          model.addAttribute("error", true);
          return "login-form";
      }
  }
  </code></pre>
  
  <p><strong>web.xml</strong></p>
  
  <pre class="lang-html prettyprint-override"><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"&gt;
  
      &lt;!-- The definition of the Root Spring Container shared by all Servlets and Filters --&gt;
      &lt;context-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;/WEB-INF/spring/root-context.xml&lt;/param-value&gt;
      &lt;/context-param&gt;
  
      &lt;context-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;/WEB-INF/application-security.xml&lt;/param-value&gt;
      &lt;/context-param&gt;
  
      &lt;!-- Creates the Spring Container shared by all Servlets and Filters --&gt;
      &lt;listener&gt;
          &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
      &lt;/listener&gt;
  
      &lt;!-- Processes application requests --&gt;
      &lt;servlet&gt;
          &lt;servlet-name&gt;appServlet&lt;/servlet-name&gt;
          &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
          &lt;init-param&gt;
              &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
              &lt;param-value&gt;/WEB-INF/spring/appServlet/servlet-context.xml&lt;/param-value&gt;
          &lt;/init-param&gt;
          &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
      &lt;/servlet&gt;
  
      &lt;filter&gt;
          &lt;filter-name&gt;springSecurityFilterChain&lt;/filter-name&gt;
          &lt;filter-class&gt;org.springframework.web.filter.DelegatingFilterProxy&lt;/filter-class&gt;
      &lt;/filter&gt;
  
      &lt;filter-mapping&gt;
          &lt;filter-name&gt;springSecurityFilterChain&lt;/filter-name&gt;
          &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
      &lt;/filter-mapping&gt;
  
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;appServlet&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  
  &lt;/web-app&gt;
  </code></pre>
  
  <p>And Finally here is my login.jsp page:</p>
  
  <pre class="lang-html prettyprint-override"><code>&lt;jsp:directive.page import="java.util.Calendar" /&gt;
  &lt;%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %&gt;
  
  &lt;div class="row"&gt;
      &lt;div class="col-sm-6 col-md-4 col-md-offset-4"&gt;
          &lt;div class="account-wall"&gt;
              &lt;img class="profile-img" src="https://lh5.googleusercontent.com/-b0-k99FZlyE/AAAAAAAAAAI/AAAAAAAAAAA/eu7opA4byxI/photo.jpg?sz=120" alt="User" /&gt;
              &lt;form class="form-signin" name="f" action="&lt;c:url value='j_spring_security_check'/&gt;" method="POST"&gt;
  
                  &lt;%-- &lt;c:if test="${not empty param.login_error}"&gt; --%&gt;
                      &lt;font color="red"&gt; ${sessionScope["SPRING_SECURITY_LAST_EXCEPTION"].message}&lt;/font&gt;
                  &lt;%-- &lt;/c:if&gt; --%&gt;
  
                  &lt;input type="text" class="form-control" placeholder="Email" name="j_username" required autofocus /&gt;
                  &lt;input type="password" class="form-control" placeholder="Password" name='j_password' required /&gt;
                  &lt;button class="btn btn-lg btn-primary btn-block" type="submit"&gt;Sign in&lt;/button&gt;
                  &lt;label class="checkbox pull-left"&gt;
                      &lt;input type="checkbox" value="remember-me"&gt;Remember me
                  &lt;/label&gt;
                  &lt;a href="#" class="pull-right need-help"&gt;Need help? &lt;/a&gt;&lt;span class="clearfix"&gt;&lt;/span&gt;
              &lt;/form&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>