Performance: SecurityFilterChain configuration
This affects the request processing speed and responsiveness by controlling how security filters are applied to incoming HTTP requests.
Jump into concepts and practice - no test required
httpSecurity .addFilterBefore(customFilter, UsernamePasswordAuthenticationFilter.class) .authorizeHttpRequests(auth -> auth.anyRequest().authenticated()) .build(); // Minimal filters with clear order and only necessary filters applied
httpSecurity.addFilterBefore(customFilter, UsernamePasswordAuthenticationFilter.class) .addFilterAfter(anotherFilter, BasicAuthenticationFilter.class) .addFilter(customFilter2); // Multiple filters added without clear order or necessity
| Pattern | Filter Count | Filter Execution Time | Request Latency Impact | Verdict |
|---|---|---|---|---|
| Many unordered filters | 5+ | High (multiple passes) | Adds 50-100ms latency | [X] Bad |
| Minimal necessary filters ordered | 1-2 | Low (single pass) | Adds 10-30ms latency | [OK] Good |
SecurityFilterChain in Spring Boot?SecurityFilterChain bean in Spring Boot?http.build().http.build(). Options B and D have wrong return types or missing annotations. @Component public void filterChain(HttpSecurity http) { http.build(); } uses @Component and void return, which is incorrect./admin URL?@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.authorizeHttpRequests(auth -> auth
.requestMatchers("/admin").hasRole("ADMIN")
.anyRequest().authenticated()
).formLogin();
return http.build();
}@Bean
public SecurityFilterChain filterChain(HttpSecurity http) {
http.authorizeHttpRequests(auth -> auth
.requestMatchers("/user").authenticated()
.anyRequest().permitAll()
);
return http.build();
}http.build() method can throw a checked exception, so the method should declare throws Exception.http.build() correctly. The order of authenticated() and permitAll() is valid. So the only issue is missing exception declaration./public/**, requires authentication for /user/**, and restricts /admin/** to users with role ADMIN, and denies access to all other requests. Which configuration snippet correctly implements this?