Security Filters and Interceptors
About
Security Filters and Interceptors are two core components in Spring Security that handle authentication, authorization, and request processing at different stages.
Component
Purpose
Executes Before/After
Security Filters
Process incoming requests for authentication, authorization, CSRF protection, CORS, etc.
Before the request reaches the controller
Security Interceptors
Perform security-related checks at the method and API level.
Before/After method execution
Security Filters in Spring Security
How Filters Work in Spring Security
Spring Security uses the Servlet Filter chain to intercept requests before they reach the application.
Security Filter Chain Overview
The Http request enters the application.
Spring Security filters the request based on authentication and authorization rules.
If the request is valid, it is passed to the next filter.
If authentication/authorization fails, the request is rejected.
Once all filters are executed, the request is handed to the application.
Core Security Filters in Spring Security
Spring Security applies multiple filters in a specific order:
Filter Name
Purpose
ChannelProcessingFilter
Enforces HTTPS and security constraints.
WebAsyncManagerIntegrationFilter
Integrates Spring Security with asynchronous requests.
SecurityContextPersistenceFilter
Restores SecurityContext
between requests (session-based security).
HeaderWriterFilter
Adds security headers (e.g., X-Frame-Options, Content-Security-Policy).
CorsFilter
Handles CORS policies.
CsrfFilter
Prevents Cross-Site Request Forgery (CSRF) attacks.
LogoutFilter
Manages user logout functionality.
UsernamePasswordAuthenticationFilter
Handles form-based authentication (processes username/password login).
DefaultLoginPageGeneratingFilter
Generates a default login page if none is provided.
BasicAuthenticationFilter
Handles HTTP Basic Authentication.
BearerTokenAuthenticationFilter
Processes JWT and OAuth2 token-based authentication.
RequestCacheAwareFilter
Saves unauthorized requests and redirects after authentication.
SecurityContextHolderAwareRequestFilter
Wraps requests to provide Spring Security capabilities.
AnonymousAuthenticationFilter
Assigns an anonymous authentication token to unauthenticated users.
SessionManagementFilter
Controls session security policies (concurrent session control, timeout).
ExceptionTranslationFilter
Converts security exceptions into HTTP responses.
FilterSecurityInterceptor
Final authorization filter that enforces security rules.
How Spring Security Applies Filters
Spring Security automatically registers filters based on the configuration.
Example: Manually Registering a Custom Security Filter
addFilterBefore()
inserts a custom filter beforeUsernamePasswordAuthenticationFilter
.Custom filters can be used for logging, request validation, or additional security checks.
Custom Security Filter Implementation
A custom security filter can inspect or modify requests before authentication.
Example: Custom Security Filter
OncePerRequestFilter
ensures the filter runs once per request.The filter logs requests before processing.
Security Interceptors in Spring Security
Security Interceptors enforce method-level security in Spring Security. They work inside the application, rather than at the filter level.
Types of Security Interceptors
Interceptor
Purpose
FilterSecurityInterceptor
Ensures authorization at the filter level.
MethodSecurityInterceptor
Enforces security on methods using annotations.
AspectJMethodSecurityInterceptor
Enables AspectJ-based security for non-Spring-managed beans.
Method Security with Security Interceptors
Using @PreAuthorize
for Authorization
@PreAuthorize
is evaluated byMethodSecurityInterceptor
before executing the method.
Using @PostAuthorize
for Authorization
The method executes first, and then authorization is checked after the result is returned.
How Spring Security Configures Interceptors
Spring Security automatically registers interceptors when method-level security is enabled.
@EnableMethodSecurity
enablesMethodSecurityInterceptor
for method-level security.
Last updated
Was this helpful?