Spring Security's AuthenticationFilter is a generic servlet filter that turns an incoming HTTP request into an Authentication object, passes it to an AuthenticationManager, and stores the result in the security context when authentication succeeds. You use it to add a custom scheme, such as an API key or a bearer token, without writing a filter from scratch.
The class lives in org.springframework.security.web.authentication, extends OncePerRequestFilter, and is separate from UsernamePasswordAuthenticationFilter, which is hard-wired to form login. This article targets Spring Security 6 (Spring Boot 3 and later), where WebSecurityConfigurerAdapter and antMatchers no longer exist.
Spring Security runs every request through a SecurityFilterChain, an ordered list of filters. Some filters handle sessions, CSRF or headers, and others handle authentication. AuthenticationFilter is the reusable building block for the last group.
For each request it follows the same steps:
AuthenticationConverter reads the request and builds an unauthenticated Authentication, or returns null when no credentials are present.AuthenticationManager (usually a ProviderManager) authenticates it by asking its AuthenticationProvider instances.SecurityContext, saves it with a SecurityContextRepository, and calls the success handler. The chain then continues.If the converter returns null, the filter does nothing and the request moves on unauthenticated. A later authorization step then decides whether anonymous access is allowed.
| Collaborator | Role | Default |
|---|---|---|
AuthenticationConverter | Extracts credentials from the request | None, you must provide it |
AuthenticationManager | Validates the token | None, you must provide it |
AuthenticationSuccessHandler | Runs after success | Does nothing, the chain continues |
AuthenticationFailureHandler | Runs after failure | Entry-point based failure handler, set your own for a clear status code |
RequestMatcher | Limits which requests are processed | All requests |
SecurityContextRepository | Persists the context | Request attribute only, so no HTTP session |
Because the converter and manager are separate objects, the same filter works for API keys, signed headers, or any scheme that fits "read credentials, then verify them".
This example authenticates requests that carry an X-API-Key header. It needs a token type, a converter, a provider, and a filter chain.
Disabling CSRF is acceptable here because the API is stateless and uses a header credential rather than cookies. Do not copy that setting to a cookie-based application.
Many tutorials, including earlier versions of this answer, extend WebSecurityConfigurerAdapter, call authorizeRequests() and antMatchers(), and import javax.servlet. All of that is obsolete:
WebSecurityConfigurerAdapter was deprecated in Spring Security 5.7 and removed in 6.0. Declare a SecurityFilterChain bean instead.authorizeRequests() and antMatchers() were replaced by authorizeHttpRequests() and requestMatchers().javax.servlet to jakarta.servlet with Spring Boot 3.UsernamePasswordAuthenticationFilter and implements Filter is redundant, and the @WebFilter annotation would register it a second time outside the security chain.addFilterBefore(filter, AuthorizationFilter.class) places the filter before the authorization decision, so the security context is filled by the time access rules run. Use addFilterAt to replace a built-in filter, or addFilterAfter to run after one. The chain needs a registered order for custom filters, which is why you anchor to an existing class.
You can verify the behaviour with curl:
The first call has no header, so the converter returns null and the authorization rule rejects it. The second authenticates. The third reaches the failure handler and returns 401 Unauthorized.
No. UsernamePasswordAuthenticationFilter is a specialised subclass of AbstractAuthenticationProcessingFilter for form login. AuthenticationFilter is a general-purpose filter you configure with your own converter and manager.
Prefer built-in support such as oauth2ResourceServer() for JWT or opaque tokens, httpBasic(), or formLogin(). Write an AuthenticationFilter only for schemes Spring Security does not cover.
A filter declared as a Spring bean is also registered by the servlet container. Either avoid exposing the filter as a bean, or add a FilterRegistrationBean with setEnabled(false). The example above creates the filter inside the chain, so it runs once.
Implement AuthenticationFailureHandler, set the status and content type on the response, and write your error body. Pass it to setFailureHandler.
AuthenticationFilter separates credential extraction (AuthenticationConverter) from verification (AuthenticationManager), which makes custom schemes short to implement. Register it with a SecurityFilterChain bean, anchor it to a known filter, set explicit failure handling, and use the Spring Security 6 APIs rather than the removed adapter-based configuration.