AuthenticationFailureHandler is the Spring Security interface that runs when a login attempt fails in a filter such as the form login filter. Its single method, onAuthenticationFailure, receives the request, the response and the AuthenticationException, so you decide what happens next: redirect with an error code, return a JSON error, log the attempt or count failures.
If you do not provide one, form login redirects to the login page with an error query parameter. A custom handler is how you change that behaviour.
The interface is in org.springframework.security.web.authentication:
Spring Security 6 and later use the jakarta.servlet types. The exception tells you why authentication failed, and its class is the reliable way to distinguish causes.
| Exception | Meaning |
|---|---|
BadCredentialsException | Wrong username or password |
LockedException | Account is locked |
DisabledException | Account is disabled |
AccountExpiredException | Account has expired |
CredentialsExpiredException | Password has expired |
InternalAuthenticationServiceException | An internal error, such as a database failure |
Checking the class is better than searching the exception message text, because messages can change or be localised.
Attach the handler in the SecurityFilterChain. This is the current style; the old WebSecurityConfigurerAdapter was removed in Spring Security 6.
Choose the response from the exception type and redirect with a short code, not free text. A code is safe to put in a URL and the login page can map it to a message.
The login page reads param.error and shows a matching message. Never put the raw exception message into the URL: it is not URL-encoded, it can leak details, and it opens the door to injected text on the login page.
For a REST backend the browser redirect is useless. Write a status code and a JSON body instead:
Use 401 for failed credentials. Keep the message generic.
Spring Security already ships useful implementations, so you often do not need a custom class:
SimpleUrlAuthenticationFailureHandler redirects (or forwards) to a fixed URL and can store the exception in the session.ForwardAuthenticationFailureHandler forwards to a URL on the server.DelegatingAuthenticationFailureHandler picks a handler by exception type, which replaces a long if chain.Failure runs when authentication fails and success runs when it succeeds. Both are set on the login configuration the same way.
Call failureHandler(...) inside formLogin(...) in a SecurityFilterChain bean. The same method exists for other login mechanisms such as oauth2Login.
Yes. Check the exception class, or use DelegatingAuthenticationFailureHandler to map exception types to handlers.
No. Authorisation failures go to the AccessDeniedHandler. This interface only covers failed authentication.
AuthenticationFailureHandler lets you control the outcome of a failed login. Register it with failureHandler(...), branch on the exception class, redirect with a short code or return JSON, use the built-in handlers when they are enough, and keep responses generic to avoid leaking account details.