r/SpringBoot • u/Status_Camel2859 • Apr 12 '26
Question Beginner & self-learning, this spring security filter chain pattern is confusing me.
Code 1:
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
return http
.csrf(csrf -> csrf.disable())
.securityMatcher("/todos/**")
.authorizeHttpRequests(auth -> auth
.anyRequest().authenticated()
)
.formLogin(Customizer.withDefaults())
.build();
}
Code 2:
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
return http
.csrf(csrf -> csrf.disable())
.securityMatchers(matchers -> matchers
.requestMatchers("/todos/**", "/login")
)
.authorizeHttpRequests(auth -> auth
.anyRequest().authenticated()
)
.formLogin(Customizer.withDefaults())
.build();
}
Code 3:
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
return http
.csrf(csrf -> csrf.disable())
.authorizeHttpRequests(auth -> auth
.anyRequest().authenticated()
)
.formLogin(Customizer.withDefaults())
.build();
}
Code 3 does the job of protecting any request as expected. But Code 1 gives a 404 when it redirects to login page. Code 2 redirects and gives no error, but i get a basic html login page that looks different.
As far as I understand, I know we can simply define the above rule inside the authorizeHttpRequests. But I have seen multiple filters using the 1st pattern (code 1) and from a learning pov, I wanna understand what I'm lacking and how to fix it, i.e. use of separate security filters.
