UsernamePasswordAuthenticationFilter is the Spring Security servlet filter that handles form login. It listens for a POST to /login, reads the username and password request parameters, wraps them in an unauthenticated UsernamePasswordAuthenticationToken, and hands that token to the AuthenticationManager. On success it stores the authenticated result and runs the success handler; on failure it runs the failure handler.
You rarely instantiate it yourself. Calling formLogin() in your SecurityFilterChain registers and configures it for you. Knowing what it does helps when you need a JSON login endpoint, extra login fields or custom redirects.
Spring Security runs a chain of filters before your controllers. UsernamePasswordAuthenticationFilter extends AbstractAuthenticationProcessingFilter, which provides the shared workflow. It is placed after SecurityContextHolderFilter and CsrfFilter, so the CSRF token has already been checked by the time credentials are read.
The filter only acts when the request matches its login matcher, which by default is POST /login. Every other request passes straight through to the next filter. A GET /login is not handled by this filter; it renders the login page produced by DefaultLoginPageGeneratingFilter or your own controller.
The flow for a single form submission is:
username and password (plus the CSRF token) to /login.AbstractAuthenticationProcessingFilter.doFilter checks whether the request needs authentication.attemptAuthentication reads the parameters and builds UsernamePasswordAuthenticationToken.unauthenticated(username, password).AuthenticationManager, normally a ProviderManager.DaoAuthenticationProvider loads the user through UserDetailsService, compares the password with the PasswordEncoder, and returns an authenticated token carrying the user's authorities.SecurityContext and calls the AuthenticationSuccessHandler. On an AuthenticationException it clears the context and calls the AuthenticationFailureHandler.The core of attemptAuthentication is short:
The username is trimmed, the password is left as submitted, and setDetails attaches the remote address and session ID as WebAuthenticationDetails.
In Spring Security 6 and later (Spring Boot 3 and 4), you declare a SecurityFilterChain bean with the lambda DSL. formLogin configures this filter.
This changes the form field names to email and secret, sets a custom login page, and always redirects to /dashboard after success. WebSecurityConfigurerAdapter, used in older tutorials, was deprecated in Spring Security 5.7 and removed in 6.0, so do not extend it.
Most needs are covered by formLogin options and handlers. When they are not, you can subclass the filter or write your own.
Handlers keep the filter unchanged and only alter what happens afterwards, which is the right choice for returning JSON from a single-page app.
To read extra data, such as a JSON body or a tenant ID, extend UsernamePasswordAuthenticationFilter and override attemptAuthentication. Register it before the standard filter and give it the shared AuthenticationManager.
When you add a custom filter, you must also set a SecurityContextRepository on it (for example HttpSessionSecurityContextRepository). Since Spring Security 6, the context is no longer saved to the session automatically, so a login that skips this step succeeds but the user is anonymous on the next request.
| Symptom | Likely cause |
|---|---|
| Login form reloads with no error | Missing CSRF token in the form, or wrong loginProcessingUrl |
Authentication method not supported: GET | Login submitted with GET while postOnly is true |
| Login works but next request is anonymous | Custom filter does not save the SecurityContext |
| Always "Bad credentials" | Stored password not encoded with the configured PasswordEncoder |
| Custom field is ignored | usernameParameter or passwordParameter does not match the form |
Setting logging.level.org.springframework.security=DEBUG prints the filter chain order and each decision, which is the fastest way to see whether the filter ran.
Not usually. Stateless APIs normally use BearerTokenAuthenticationFilter from the OAuth 2.0 resource server support. You can still use a custom subclass for a token-issuing login endpoint.
Yes. Use loginProcessingUrl("/auth/login") for the URL the filter listens on and loginPage(...) for the page that shows the form.
No. It only collects credentials. The AuthenticationManager and its providers verify them.
You can call setPostOnly(false) on a custom filter, but sending passwords in a URL is unsafe and logs them. Keep POST.
UsernamePasswordAuthenticationFilter turns a submitted login form into an authentication request. It extracts the credentials, delegates verification to the AuthenticationManager, and triggers the success or failure handler. Configure it through formLogin in a SecurityFilterChain bean, customize behaviour with handlers first, and only subclass it when you need to read credentials from a different source.