title: Spring Security need to redirect to Login Page on BadCredentialsException
body: <p>I am new to Spring Security.
  I am facing one issue, I need error to get Displayed on Login page when Authentication fail for specific reason.</p>
  
  <p>Also, I am authenticating user based on Web Service call to URL Configured.
  Below is my configuration, Also I have configured <code>authentication-failure-handler-ref</code></p>
  
  <pre><code>&lt;security:http auto-config="true" use-expressions="true" access-denied-page="/accessDenied.jsp"&gt;
      &lt;security:form-login  login-page="/login.jsp" authentication-failure-handler-ref="authenticationFailureHandler"
          default-target-url="/jsp/home"   /&gt;
  
      &lt;security:intercept-url pattern="/login.jsp"
          access="permitAll" /&gt;
  
      &lt;security:intercept-url pattern="/jsp/home"
          access="permitAll" /&gt;
  
      &lt;security:intercept-url pattern="/jsp/*"
          access="isAuthenticated()" /&gt;
  
  
      &lt;security:logout logout-url="/j_spring_security_logout" logout-success-url="/login.jsp?logout=success" 
          invalidate-session="true"  /&gt;
  
  &lt;/security:http&gt; 
  
      &lt;bean id="ldapAuthenticationProvider" class="com.on.transport.authentication.MyAuthenticationProvider"&gt;
      &lt;/bean&gt;
  
      &lt;security:authentication-manager alias="authenticationManager"&gt;
           &lt;security:authentication-provider ref='ldapAuthenticationProvider'/&gt;   
      &lt;/security:authentication-manager&gt;
  
      &lt;bean id="customUserDetailsService" class="com.service.CustomUserDetailsService"&gt;
      &lt;/bean&gt;     
  
  &lt;bean id="authenticationFailureHandler" class="org.springframework.security.web.authentication.ExceptionMappingAuthenticationFailureHandler"&gt;
      &lt;property name="exceptionMappings"&gt;
          &lt;props&gt;
              &lt;prop key="org.springframework.security.authentication.BadCredentialsException"&gt;/errorPage.jsp?login_error=1&lt;/prop&gt;
              &lt;prop key="org.springframework.security.authentication.CredentialsExpiredException"&gt;/login.jsp?status=2&lt;/prop&gt;
              &lt;prop key="org.springframework.security.authentication.LockedException"&gt;/login.jsp?status=3&lt;/prop&gt;
              &lt;prop key="org.springframework.security.authentication.DisabledException"&gt;/login.jsp?status=4&lt;/prop&gt;     
          &lt;/props&gt;
      &lt;/property&gt;
  &lt;/bean&gt; 
  </code></pre>
  
  <p>When I write " authentication-failure-url" like below then everything works fine, </p>
  
  <pre><code>&lt;security:http  ......&gt;
          &lt;security:form-login  login-page="/login.jsp"
              default-target-url="/jsp/home"  authentication-failure-url="/login.jsp?status=1" /&gt;
                    ............
  &lt;/security:http&gt;
  </code></pre>
  
  <h2>But I need to configure in a way where I can check different type of Exceptions.</h2>
  
  <p>AuthenticationProvider</p>
  
  <pre><code>   public class MyAuthenticationProvider implements AuthenticationProvider {
  
      public Authentication authenticate(Authentication authentication)
              throws AuthenticationException {
  
          UsernamePasswordAuthenticationToken auth = (UsernamePasswordAuthenticationToken) authentication;
  
          String username = auth.getName();
          String password = (String)auth.getCredentials();
  
          UserUpdated user = null;
          int status=0;
          try{
              status = webservice.authenticate(username, password); 
                 //webservice is object one is used to authenticate
  
              if(status==1){
                  List&lt;GrantedAuthority&gt; grantedAuthoritiesList = new ArrayList&lt;GrantedAuthority&gt;();
  
                  if(username!=null &amp;&amp; (username.equals("test") || username.equals("jp@on.com"))){
                      grantedAuthoritiesList.add(new GrantedAuthorityImpl("ROLE_ADMIN")); 
                  }else{
                      grantedAuthoritiesList.add(new GrantedAuthorityImpl("ROLE_USER"));
                  }
  
                  return new UsernamePasswordAuthenticationToken(username, null, grantedAuthoritiesList);
  
              }else if(status==2){
                  throw new BadCredentialsException("Incorrect Email Id or Password.");
              }
          }catch (Exception e) {
              e.printStackTrace();
          }
          return null;
      }
  
      @Override
      public boolean supports(Class&lt;? extends Object&gt; authentication) {
          return (UsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication));
      }
  }
  </code></pre>
  
  <p>I am getting error message as:</p>
  
  <pre><code>HTTP Status 401 - Authentication Failed: No AuthenticationProvider found for org.springframework.security.authentication.UsernamePasswordAuthenticationToken
  </code></pre>
  
  <p>instead I need is to redirect to Login page with error message.</p>
  
  <p>I can see Logs as </p>
  
  <pre><code>org.springframework.security.authentication.BadCredentialsException: Incorrect Email Id or Password
      at com.on.transport.authentication.MyAuthenticationProvider.authenticate(MyAuthenticationProvider.java:98)
      at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:156)
      at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:174)
      at org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter.attemptAuthentication(UsernamePasswordAuthenticationFilter.java:94)
      at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:195)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
      at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
      at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
      at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
  
  [AbstractAuthenticationProcessingFilter.unsuccessfulAuthentication] - Authentication request failed: org.springframework.security.authentication.ProviderNotFoundException: No AuthenticationProvider found for org.springframework.security.authentication.UsernamePasswordAuthenticationToken
  2013-09-07 13:05:42,685 DEBUG http-8080-2 [AbstractAuthenticationProcessingFilter.unsuccessfulAuthentication] - Updated SecurityContextHolder to contain null Authentication
  2013-09-07 13:05:42,686 DEBUG http-8080-2 [AbstractAuthenticationProcessingFilter.unsuccessfulAuthentication] - Delegating to authentication failure handler org.springframework.security.web.authentication.ExceptionMappingAuthenticationFailureHandler@3aabc1
  2013-09-07 13:05:42,686 DEBUG http-8080-2 [SimpleUrlAuthenticationFailureHandler.onAuthenticationFailure] - No failure URL set, sending 401 Unauthorized error
  2013-09-07 13:05:42,686 DEBUG http-8080-2 [HttpSessionSecurityContextRepository$SaveToSessionResponseWrapper.saveContext] - SecurityContext is empty or contents are anonymous - context will not be stored in HttpSession.
  2013-09-07 13:05:42,687 DEBUG http-8080-2 [SecurityContextPersistenceFilter.doFilter] - SecurityContextHolder now cleared, as request processing completed
  </code></pre>
  
  <p>Please help me identifying what I am missing.</p>