Understanding the Spring Security Filter Chain
Look under the hood of Spring Security 6 to understand how the servlet filter chain processes every request and where authentication fits in.
Understanding the Spring Security Filter Chain is a free Spring Security 6 & JWT Authentication lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Spring Security 6 & JWT Authentication learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
How Requests Get Secured
So how does every request actually get checked? The security filter chain — a series of servlet filters Spring slots in before your controllers.
What Is a Servlet Filter?
A servlet Filter intercepts HTTP requests and responses before they reach your code. Spring Security is built almost entirely from these filters.
The DelegatingFilterProxy
The real servlet filter, DelegatingFilterProxy, hands each request to a Spring-managed bean — bridging the servlet world and the Spring context.
The FilterChainProxy
Behind that proxy sits FilterChainProxy, which holds one or more SecurityFilterChain instances and routes each request to the one that matches.
Key Filters in Order
Filters run in a fixed order: SecurityContextHolderFilter loads context, the auth filter handles login, and AuthorizationFilter enforces access rules.
Defining a SecurityFilterChain Bean
In Spring Security 6 you configure everything by declaring a SecurityFilterChain bean — the modern replacement for WebSecurityConfigurerAdapter. See below.
@Bean
SecurityFilterChain chain(HttpSecurity http) throws Exception {
http.authorizeHttpRequests(a -> a.anyRequest().authenticated())
.formLogin(Customizer.withDefaults());
return http.build();
}Where the SecurityContext Lives
After login, the Authentication is stored in the SecurityContext and stays reachable via SecurityContextHolder for the rest of the request.
Authentication auth = SecurityContextHolder.getContext().getAuthentication();Permitting Some Paths
Let public paths through while securing the rest — all on the same chain. The code uses permitAll() for /public and authenticated() for everything else.
http.authorizeHttpRequests(a -> a
.requestMatchers('/public/**').permitAll()
.anyRequest().authenticated());Multiple Filter Chains
Register several SecurityFilterChain beans with securityMatcher so API and web paths get different rules. The first matching chain wins.
http.securityMatcher('/api/**');Adding a Custom Filter
Slot your own filter at a precise position with addFilterBefore — the foundation for the JWT processing you'll build later in this course.
http.addFilterBefore(myFilter, UsernamePasswordAuthenticationFilter.class);Why This Matters
Knowing the chain explains why ordering matters, where auth versus authz happens, and exactly where a custom JWT filter has to plug in.
Quick Check
In Spring Security 6, how do you define your security configuration?
Recap
Recap: requests flow DelegatingFilterProxy to FilterChainProxy to SecurityFilterChain; filters run in order, auth then authz, and addFilterBefore inserts custom ones.
Frequently asked questions
Is the “Understanding the Spring Security Filter Chain” lesson free?
Yes — the full text of “Understanding the Spring Security Filter Chain” is free to read here on the web, and the Spring Security 6 & JWT Authentication course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Spring Security 6 & JWT Authentication course, upgrade to CoddyKit PRO.
What will I learn in “Understanding the Spring Security Filter Chain”?
Look under the hood of Spring Security 6 to understand how the servlet filter chain processes every request and where authentication fits in. You practise Spring Security 6 & JWT Authentication with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Spring Security 6 & JWT Authentication?
No prior experience is required. Spring Security 6 & JWT Authentication on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Understanding the Spring Security Filter Chain” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Spring Security 6 & JWT Authentication lesson?
Yes. Every Spring Security 6 & JWT Authentication lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Introduction to Spring Security 6
- Project Setup and Dependencies
- In-Memory User Authentication
- Understanding the Spring Security Filter Chain