title: Spring Security : Redirecting to login page if the authentication failed
body: <p>We are having two ways of logging in.</p>
  
  <p>
      <li>The user name and password are sent by another app in the request headers. IT is examined and if the user name and password are correct, it goes in. [A custom filter is written for this]</li>
       <li>If the user name and password are not present in the request headers, the login screen is presented. </li>
  </p>
  
  <p>When the user name and password are present in the request header and if it's wrong, I am shown an <strong>HTTP Status 401 - Authentication Failed: Bad credentials</strong> page. </p>
  
  <p>How do I make it show the login page in case the authentication failed?</p>
  
  <p>Below is the code in the security.xml</p>
  
  <pre><code>    &lt;http auto-config="true" use-expressions="true"&gt;
               &lt;access-denied-handler error-page="/login.jsp"/&gt;
              &lt;intercept-url pattern="/*Login*" access="hasRole('ROLE_ANONYMOUS')"/&gt;
              &lt;intercept-url pattern="/*" access="hasRole('ROLE_USER') or hasRole('ROLE_ADMIN')"/&gt;
              &lt;custom-filter ref="requestHeaderFilter" before="FORM_LOGIN_FILTER"/&gt;
              &lt;form-login login-page="/login.jsp"/&gt;
  
      &lt;/http&gt;
  </code></pre>
  
  <p>Please let me know if you need more information.</p>
  
  <p>Edit: Adding the code for RequestHeader filter in my application </p>
  
  <pre><code>public class RequestHeaderProcessingFilter extends AbstractAuthenticationProcessingFilter{
  
  private String usernameHeader = "j_username";
  private String passwordHeader = "j_password";
  
  
  protected RequestHeaderProcessingFilter() {
      super("/login_direct");
   }
  
  //getters and setters
  
  @Override
  public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException, IOException, ServletException {
      String username = request.getHeader(usernameHeader);
      String password = request.getHeader(passwordHeader);
  
       SignedUsernamePasswordAuthenticationToken authRequest =
          new SignedUsernamePasswordAuthenticationToken(username, password);
  
        return this.getAuthenticationManager().authenticate(authRequest); 
  }
  </code></pre>
  
  <p>}</p>