What is the purpose of the Spring Security filter chain, and how do you add or customize a filter in it?

The Spring Security filter chain is an ordered sequence of servlet filters that each handle one aspect of request processing—like extracting credentials, checking CSRF tokens, or enforcing authorization—before the request reaches the application's controllers.

Key Points

Example

A custom JwtAuthFilter is added with addFilterBefore(jwtAuthFilter, UsernamePasswordAuthenticationFilter.class) so token validation happens before Spring Security's standard authentication processing runs.

@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
    http.addFilterBefore(jwtAuthFilter, UsernamePasswordAuthenticationFilter.class);
    return http.build();
}

Common Mistakes

Follow-up Questions

Interview Tip

A concise interview answer is

The filter chain is an ordered pipeline of filters, each responsible for one piece of security processing like authentication or authorization, that a request passes through before reaching the controller. To add a custom filter—say for JWT validation—I'd use addFilterBefore() or addFilterAfter() to place it precisely relative to an existing filter.

Last updated: 11 November 2026

Previous question: What is the best practice for storing passwords in a Spring Security application?

Next question: How does Spring Security handle session management, and what are the options for handling concurrent sessions?

More Spring Security - Level I interview questions

All Spring Security - Level I questions