๐ก๏ธ Behind the Scenes of Spring Security: Mastering SecurityFilterChain Like a Pro
Spring Security has evolvedโโโthe old WebSecurityConfigurerAdapter is gone, and the new SecurityFilterChain is now the backbone of Springโฆ

๐ก๏ธ โBehind the Scenes of Spring Security: Mastering SecurityFilterChain Like a Proโ
๐ก๏ธ Behind the Scenes of Spring Security: Mastering SecurityFilterChain Like a Pro
Spring Security has evolved โ the old WebSecurityConfigurerAdapter is gone, and the new SecurityFilterChain is now the backbone of Spring Boot security configuration.
If youโve ever wondered what happens behind the curtain when a request hits your secure API, or how filters decide whether to allow or deny access โ this guide is for you. ๐
Letโs break down what SecurityFilterChain really is, why it replaced the old approach, and how to configure it the right way.
๐ What Is a SecurityFilterChain?
Think of it as a sequence of filters that inspect and process every incoming HTTP request before it reaches your controllers.
Each filter has one job:
- Authenticate users ๐งโ๐ป
- Check permissions ๐
- Handle CSRF tokens ๐งพ
- Redirect or deny unauthorized access ๐ซ
Together, these filters form a chain โ the SecurityFilterChain.
In short:
๐ Every request goes through the chain, and each filter decides what happens next.
โ๏ธ Why WebSecurityConfigurerAdapter Was Replaced
Before Spring Security 5.7, we used this pattern ๐
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/public/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin();
}
}
That worked fine โ but it was:
- Hard to test (tight coupling)
- Not modular
- Verbally heavy
Now, the modern Spring Security style uses beans instead. ๐ก
๐งฉ The New Way โ Defining SecurityFilterChain as a Bean
Hereโs the new (and preferred) approach ๐
@Configuration
@EnableWebSecurity
public class SecurityConfig {
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.csrf(csrf -> csrf.disable()) // disable CSRF for simplicity
.authorizeHttpRequests(auth -> auth
.requestMatchers("/public/**").permitAll() // public APIs
.requestMatchers("/admin/**").hasRole("ADMIN") // restricted APIs
.anyRequest().authenticated() // everything else
)
.formLogin(Customizer.withDefaults()) // enable form login
.httpBasic(Customizer.withDefaults()); // enable basic auth
return http.build();
}
}
โ Advantages:
- Uses modern functional style
- Supports multiple filter chains
- Easier to maintain and test
๐ง How It Works Internally
When you start your Spring Boot app:
- Spring Security creates a filter chain proxy (
FilterChainProxy). - Each request is checked against a list of configured
SecurityFilterChains. - The first chain that matches handles the request.
- Filters execute in order (e.g., CORS โ CSRF โ Authentication โ Authorization).
If no chain matches, the request is processed as unsecured (open endpoint).
๐ก You can have multiple SecurityFilterChain beans, each handling a different path.
๐ Example: Multiple SecurityFilterChains
Letโs say you want different rules for /api/** and /admin/**.
@Configuration
@EnableWebSecurity
public class MultiSecurityConfig {@Bean
@Order(1)
public SecurityFilterChain adminChain(HttpSecurity http) throws Exception {
http
.securityMatcher("/admin/**")
.authorizeHttpRequests(auth -> auth
.anyRequest().hasRole("ADMIN")
)
.formLogin(Customizer.withDefaults());
return http.build();
}
@Bean
@Order(2)
public SecurityFilterChain apiChain(HttpSecurity http) throws Exception {
http
.securityMatcher("/api/**")
.authorizeHttpRequests(auth -> auth
.anyRequest().authenticated()
)
.httpBasic(Customizer.withDefaults());
return http.build();
}
}
โ
Each SecurityFilterChain applies only to matching URLs.
๐ง Order matters โ Spring picks the first matching chain.
๐งพ Adding a Custom Filter
Want to add your own logic (e.g., logging or token validation)? You can easily inject a custom filter into the chain.
@Component
public class RequestLoggingFilter extends OncePerRequestFilter {
@Override
protected void doFilterInternal(HttpServletRequest request,
HttpServletResponse response,
FilterChain filterChain)
throws ServletException, IOException {
System.out.println("๐ Incoming request: " + request.getRequestURI());
filterChain.doFilter(request, response);
}
}
Then, plug it into your chain:
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.addFilterBefore(new RequestLoggingFilter(), UsernamePasswordAuthenticationFilter.class)
.authorizeHttpRequests(auth -> auth.anyRequest().authenticated())
.httpBasic(Customizer.withDefaults());
return http.build();
}
โ Your custom filter executes before authentication. Perfect for request logging, rate limiting, or custom headers.
๐งฉ Bonus Tip: Testing Your Security Config
You can write quick tests using Springโs MockMvc to ensure your security behaves correctly.
@SpringBootTest
@AutoConfigureMockMvc
class SecurityTests {
@Autowired
private MockMvc mockMvc;
@Test
void whenAccessPublic_thenOk() throws Exception {
mockMvc.perform(get("/public/hello"))
.andExpect(status().isOk());
}
@Test
void whenAccessAdminWithoutAuth_thenUnauthorized() throws Exception {
mockMvc.perform(get("/admin/panel"))
.andExpect(status().isUnauthorized());
}
}
โ
Helps you confirm that your SecurityFilterChain behaves as expected.
๐ฎ The Future of Spring Security Config
Spring Securityโs move to SecurityFilterChain is part of a functional, declarative trend:
- Less boilerplate ๐งฑ
- More composable configurations
- Easier testing & multiple chain support
If youโve been using WebSecurityConfigurerAdapter, itโs time to embrace the future โ cleaner, leaner, and more powerful.
๐ TL;DR โ Key Takeaways

๐ฌ Final Thought
Spring Security isnโt just a firewall โ itโs a framework built around filter orchestration.
Once you understand SecurityFilterChain, you understand how every request in your app is secured.
โSecurity isnโt a plugin โ itโs a mindset. ๐โ
A message from our Founder
Hey, Sunil here. I wanted to take a moment to thank you for reading until the end and for being a part of this community.
Did you know that our team run these publications as a volunteer effort to over 3.5m monthly readers? We donโt receive any funding, we do this to support the community. โค๏ธ
If you want to show some love, please take a moment to follow me on LinkedIn, TikTok, **Instagram. You can also subscribe to our [weekly newsletter](https://newsletter.plainenglish.io/)**.
And before you go, donโt forget to clap and follow the writer๏ธ!
๋ฉํ๋ฐ์ดํฐ
- post_id
- dab68f96fecc
- slug
- ๏ธ-behind-the-scenes-of-spring-security-mastering-securityfilterchain-like-a-pro-dab68f96fecc
- url
- https://blog.stackademic.com/%EF%B8%8F-behind-the-scenes-of-spring-security-mastering-securityfilterchain-like-a-pro-dab68f96fecc
- canonical_url
- https://blog.stackademic.com/%EF%B8%8F-behind-the-scenes-of-spring-security-mastering-securityfilterchain-like-a-pro-dab68f96fecc
- author_url
- https://medium.com/@lakshitagangola123
- status
- ok
- fetched_at
- 2026-07-16 21:00:47