A security filter chain in Spring Security is an ordered list of servlet filters that every matching HTTP request passes through before it reaches your controllers. Each filter handles one concern: loading the security context, CSRF protection, authentication, exception translation or authorisation. In Spring Security 6 and 7 you define one or more chains as SecurityFilterChain beans.
Spring registers a single servlet filter, DelegatingFilterProxy (in Spring Boot, a FilterChainProxy registered by the auto-configuration), which picks the first chain whose request matcher fits the request and runs that chain's filters in order.
The request flow looks like this:
springSecurityFilterChain, which is a FilterChainProxy.FilterChainProxy asks each SecurityFilterChain whether it matches the request, in the order of their @Order values.DispatcherServlet and your controller.AuthorizationFilter throws an AccessDeniedException and the response becomes a 401 or 403.Only one chain handles a request. Filters in other chains do not run.
Since Spring Security 5.7 the WebSecurityConfigurerAdapter is deprecated, and it is removed in 6.0. You declare the chain as a bean and configure it with the lambda DSL:
HttpSecurity is a builder. Each method call adds, configures or removes filters, and http.build() produces the immutable SecurityFilterChain. Disable CSRF protection only when the API does not use cookie-based sessions.
The order is fixed by Spring Security, so you can reason about what runs before what. The list below is a simplified view of the default servlet stack:
| Order (approx.) | Filter | Purpose |
|---|---|---|
| Early | DisableEncodeUrlFilter | Stops session IDs being written into URLs |
| Early | SecurityContextHolderFilter | Loads the SecurityContext for the request |
| Early | HeaderWriterFilter | Adds security headers such as X-Content-Type-Options |
| Early | CorsFilter | Applies CORS rules when configured |
| Early | CsrfFilter | Validates CSRF tokens on unsafe methods |
| Middle | LogoutFilter | Handles the logout URL |
| Middle | UsernamePasswordAuthenticationFilter | Handles form login |
| Middle | BasicAuthenticationFilter | Handles HTTP Basic credentials |
| Late | ExceptionTranslationFilter | Turns security exceptions into 401/403 or a login redirect |
| Late | AuthorizationFilter | Applies the authorizeHttpRequests rules |
You can print the actual chain by setting logging.level.org.springframework.security=DEBUG in application.properties. Spring logs the filters of the matched chain on the first request. The exact set varies with your configuration and version.
To secure a REST API with bearer tokens, the simplest option is the built-in resource server support, which adds BearerTokenAuthenticationFilter for you:
This needs spring-boot-starter-oauth2-resource-server and a property such as spring.security.oauth2.resourceserver.jwt.issuer-uri. Prefer it over a hand-written JWT filter because validation, key rotation and clock skew are already handled.
If you do need your own filter, extend OncePerRequestFilter and place it relative to a known filter:
Use addFilterBefore, addFilterAfter or addFilterAt. The example hard-codes a key only for brevity; in real code compare against a stored hash in constant time.
Larger applications often need different rules for different URL spaces, for instance a stateless API and a form-login admin area. Declare several beans and give each a request matcher and an @Order:
The chain with the lowest order that matches wins. The last chain should have no securityMatcher so it catches everything else. Two chains without an explicit order or with overlapping matchers produce a startup error or surprising behaviour.
A servlet filter is a single component in the container. The security filter chain is a list of Spring-managed filters held inside one servlet filter, FilterChainProxy, so the filters can be selected per request and use Spring beans.
Use permitAll() in authorizeHttpRequests so the filters still run, which keeps security headers and logging intact. Avoid WebSecurityCustomizer ignoring, which bypasses the whole chain.
If the filter is a Spring bean, Spring Boot also registers it as a normal servlet filter. Either do not expose it as a bean or add a FilterRegistrationBean with setEnabled(false).
Annotations such as @PreAuthorize work through AOP, after the request has passed the filter chain, so they complement the chain rather than replace it.
The security filter chain is Spring Security's request pipeline: FilterChainProxy selects one SecurityFilterChain bean per request and runs its ordered filters. Define chains with HttpSecurity and the lambda DSL, use the resource server support for JWT, add custom filters relative to built-in ones, and use securityMatcher with @Order when different parts of the application need different rules.