Simple definition#
Spring Security protects applications with a chain of servlet filters that runs before your controllers. The filters authenticate the caller (who are you?), store the result in a SecurityContext, and authorize the request (are you allowed?). It is configured with one or more SecurityFilterChain beans.
The problem#
Security checks scattered across controllers are inconsistent and easy to forget. Attacks such as CSRF, session fixation and clickjacking need defaults most developers would not think to add. Spring Security centralises these controls, secure by default, in front of the application.
Architecture#
flowchart LR
REQ["HTTP request"] --> DFP["DelegatingFilterProxy<br/>(servlet filter)"] --> FCP["FilterChainProxy<br/>(springSecurityFilterChain bean)"]
FCP -->|"first matching chain"| C1["SecurityFilterChain /api/**<br/>stateless JWT"]
FCP --> C2["SecurityFilterChain /**<br/>form or OIDC login"]
C1 --> APP["DispatcherServlet → controller"]DelegatingFilterProxybridges the servlet container and Spring beans.FilterChainProxypicks the firstSecurityFilterChainwhose request matcher matches, then runs its filters.- You can have several chains: an API chain with JWT and a web chain with login, each with its own rules.
Open the interactive diagram: Spring Security filter chain for a JWT API Standalone page with zoom, light/dark themes and SVG/PNG export, generated with Archify. Diagram source.
Key filters, in order (simplified)#
| Filter | Job |
|---|---|
SecurityContextHolderFilter | Loads the SecurityContext (from the session, or empty for stateless) |
HeaderWriterFilter | Security headers: HSTS, X-Content-Type-Options, frame options, CSP if configured |
CorsFilter | CORS preflight and headers |
CsrfFilter | CSRF token check for state-changing requests (browser sessions) |
LogoutFilter | Logout handling |
Authentication filters: UsernamePasswordAuthenticationFilter, BearerTokenAuthenticationFilter, OAuth2LoginAuthenticationFilter, BasicAuthenticationFilter… | Extract credentials and authenticate |
RequestCacheAwareFilter, SecurityContextHolderAwareRequestFilter | Saved requests, servlet API integration |
AnonymousAuthenticationFilter | An anonymous principal when nobody is authenticated |
ExceptionTranslationFilter | Turns AuthenticationException into 401 (or a login redirect) and AccessDeniedException into 403 |
AuthorizationFilter | Applies the authorizeHttpRequests rules using an AuthorizationManager |
Turn on logging.level.org.springframework.security=TRACE to see the exact chain and the decisions for each request.
Authentication internals#
flowchart LR
F["Auth filter<br/>(extracts credentials)"] --> AM["AuthenticationManager<br/>(ProviderManager)"]
AM --> P1["DaoAuthenticationProvider<br/>UserDetailsService + PasswordEncoder"]
AM --> P2["JwtAuthenticationProvider<br/>JwtDecoder"]
P1 & P2 --> AUTH["Authenticated Authentication<br/>(principal + authorities)"] --> SC["SecurityContextHolder"]Authenticationholds the principal, credentials andGrantedAuthoritys (roles and scopes).SecurityContextHolderstores it per request (a ThreadLocal by default). Read it with@AuthenticationPrincipalin controllers.- Passwords: never store them in plain text. Use
PasswordEncoderFactories.createDelegatingPasswordEncoder(), which defaults to bcrypt and supports Argon2 and PBKDF2 with{id}prefixes for upgrades.
A modern configuration#
@Configuration
@EnableWebSecurity
@EnableMethodSecurity
class SecurityConfig {
@Bean
@Order(1)
SecurityFilterChain api(HttpSecurity http) throws Exception {
return http
.securityMatcher("/api/**")
.authorizeHttpRequests(auth -> auth
.requestMatchers(HttpMethod.GET, "/api/products/**").permitAll()
.requestMatchers("/api/admin/**").hasRole("ADMIN")
.requestMatchers(HttpMethod.POST, "/api/orders/**").hasAuthority("SCOPE_orders:write")
.anyRequest().authenticated()) // deny by default
.oauth2ResourceServer(o -> o.jwt(Customizer.withDefaults())) // see the OAuth2 article
.sessionManagement(s -> s.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.csrf(csrf -> csrf.disable()) // stateless API with bearer tokens only
.build();
}
@Bean
@Order(2)
SecurityFilterChain web(HttpSecurity http) throws Exception {
return http
.authorizeHttpRequests(a -> a.requestMatchers("/", "/css/**").permitAll().anyRequest().authenticated())
.oauth2Login(Customizer.withDefaults()) // OIDC login; CSRF stays ON
.build();
}
}
CSRF and CORS#
- CSRF protects cookie-based browser sessions. Keep it enabled for apps with session cookies (including BFFs). Disable it only for stateless APIs that accept only
Authorizationheader tokens. - CORS controls which browser origins may call your API. Configure a
CorsConfigurationSourcebean, never*with credentials.
Common mistakes#
Quick check#
What does FilterChainProxy do? It selects the first SecurityFilterChain whose matcher fits the request and runs that chain’s security filters.
Where is the authenticated user stored during a request?
In the SecurityContext, held by SecurityContextHolder (thread-bound by default).
What does ExceptionTranslationFilter do? It converts authentication failures into 401 (or a login redirect) and access-denied failures into 403.
When is it safe to disable CSRF? For stateless APIs that authenticate only with bearer tokens in headers, never with cookies.
What did Spring Security 7 add for MFA?
Built-in multi-factor authentication: factors are recorded as authorities, and @EnableMultiFactorAuthentication requires them.
Summary#
- Spring Security is a filter chain in front of MVC: authenticate → SecurityContext → authorize.
- Use separate SecurityFilterChains per area (API vs web), with deny-by-default rules.
- Use delegating password encoders, keep CSRF for cookie sessions, and configure CORS explicitly.
- Spring Security 7: lambda DSL only, built-in MFA, Authorization Server included.
References#
- Spring Security reference, Architecture and What’s New in 7.0.
- Spring Security reference, Password storage.
- OWASP, Cross-Site Request Forgery Prevention Cheat Sheet.