How does Spring Security integrate with Spring MVC?

Spring Security integrates with Spring MVC as a chain of servlet filters that sit in front of DispatcherServlet, intercepting every request to enforce authentication and authorization before it ever reaches a controller.

Key Points

Example

A request to /admin/dashboard passes through the security filter chain first; if the user isn't authenticated they're redirected to a login page before DispatcherServlet, let alone the admin controller, ever sees the request.

@Configuration
@EnableWebSecurity
public class SecurityConfig {
    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http.authorizeHttpRequests(auth -> auth
                .requestMatchers("/admin/**").hasRole("ADMIN")
                .anyRequest().authenticated())
            .formLogin(Customizer.withDefaults());
        return http.build();
    }
}

Interview Tip

A concise interview answer is

Spring Security plugs in as a filter chain in front of DispatcherServlet, so it can authenticate and authorize requests before they ever reach a controller. I enable it with @EnableWebSecurity and define a SecurityFilterChain bean that declares which URL patterns need authentication or specific roles.

Last updated: 16 July 2026

Previous question: What are the options for implementing security in a Spring MVC application?

Next question: What are the common challenges when securing a Spring MVC application?

More Spring MVC (Optional) interview questions

All Spring MVC (Optional) questions