Last updated
Was this helpful?
Last updated
Was this helpful?
Security Filters and Interceptors are two core components in Spring Security that handle authentication, authorization, and request processing at different stages.
Spring Security uses the Servlet Filter chain to intercept requests before they reach the application.
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.
Spring Security applies multiple filters in a specific order:
Spring Security automatically registers filters based on the configuration.
Example: Manually Registering a Custom Security Filter
addFilterBefore()
inserts a custom filter before UsernamePasswordAuthenticationFilter
.
Custom filters can be used for logging, request validation, or additional security checks.
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 enforce method-level security in Spring Security. They work inside the application, rather than at the filter level.
Using @PreAuthorize
for Authorization
@PreAuthorize
is evaluated by MethodSecurityInterceptor
before executing the method.
Using @PostAuthorize
for Authorization
The method executes first, and then authorization is checked after the result is returned.
Spring Security automatically registers interceptors when method-level security is enabled.
@EnableMethodSecurity
enables MethodSecurityInterceptor
for method-level security.
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
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.
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.