title: Spring custom AuthenticationFailureHandler
body: <p>I already try the whole day, to get my custom authentication failure handler to work with Spring 3.1.3.</p>
  
  <p>I think it is properly configured</p>
  
  <pre><code>&lt;http use-expressions="true" disable-url-rewriting="true"&gt;
      &lt;intercept-url pattern="/rest/login" access="permitAll" /&gt;
      &lt;intercept-url pattern="/rest/**" access="isAuthenticated()" /&gt;
      &lt;intercept-url pattern="/index.html" access="permitAll" /&gt;
      &lt;intercept-url pattern="/js/**" access="permitAll" /&gt;
      &lt;intercept-url pattern="/**" access="denyAll" /&gt;
      &lt;form-login username-parameter="user" password-parameter="pass" login-page="/rest/login"
          authentication-failure-handler-ref="authenticationFailureHandler"  /&gt;
  &lt;/http&gt;
  &lt;beans:bean id="authenticationFailureHandler" class="LoginFailureHandler" /&gt;
  </code></pre>
  
  <p>My implementation is this</p>
  
  <pre><code>public class LoginFailureHandler implements AuthenticationFailureHandler {
      private static final Logger log = LoggerFactory.getLogger(LoginFailureHandler.class);
  
      public LoginFailureHandler() {
          log.debug("I am");
      }
  
      @Autowired
      private ObjectMapper customObjectMapper;
  
      @Override
      public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
              AuthenticationException exception) throws IOException, ServletException {
          log.debug("invalid login");
          User user = new User();
          user.setUsername("invalid");
          try (OutputStream out = response.getOutputStream()) {
              customObjectMapper.writeValue(out, user);
          }
      }
  
  }
  </code></pre>
  
  <p>In the console I see</p>
  
  <pre><code>2013-04-11 14:52:29,478 DEBUG LoginFailureHandler - I am
  </code></pre>
  
  <p>So it is loaded.</p>
  
  <p>With wrong username or passwort, when a BadCredentialsException is thrown, I don't see invalid login.</p>
  
  <p>The Method onAuthenticationFailure is never invoked.</p>
  
  <p>Instead the service redirects the browser onto /rest/login again and again...</p>
  
  <p><strong>Edit</strong></p>
  
  <pre><code>2013-04-11 15:47:26,411 DEBUG de.pentos.spring.LoginController - Incomming login chuck.norris, norris
  2013-04-11 15:47:26,412 DEBUG o.s.s.a.ProviderManager - Authentication attempt using org.springframework.security.authentication.dao.DaoAuthenticationProvider
  2013-04-11 15:47:26,415 DEBUG o.s.s.a.d.DaoAuthenticationProvider - Authentication failed: password does not match stored value
  2013-04-11 15:47:26,416 DEBUG o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver - Resolving exception from handler [public de.pentos.spring.User de.pentos.spring.LoginController.login(de.pentos.spring.User)]: org.springframework.security.authentication.BadCredentialsException: Bad credentials
  2013-04-11 15:47:26,419 DEBUG o.s.w.s.m.a.ResponseStatusExceptionResolver - Resolving exception from handler [public de.pentos.spring.User de.pentos.spring.LoginController.login(de.pentos.spring.User)]: org.springframework.security.authentication.BadCredentialsException: Bad credentials
  2013-04-11 15:47:26,419 DEBUG o.s.w.s.m.s.DefaultHandlerExceptionResolver - Resolving exception from handler [public de.pentos.spring.User de.pentos.spring.LoginController.login(de.pentos.spring.User)]: org.springframework.security.authentication.BadCredentialsException: Bad credentials
  2013-04-11 15:47:26,426 DEBUG o.s.web.servlet.DispatcherServlet - Could not complete request
  org.springframework.security.authentication.BadCredentialsException: Bad credentials
  </code></pre>
  
  <p>This happens in DEBUG Mode</p>
  
  <p>Where is my mistake?</p>