@EnableWebMvcSecurity was a Spring Security annotation that enabled web security together with Spring MVC integration. It was deprecated in Spring Security 4.0 and removed in a later release, and it does not exist in Spring Security 5 or 6. The replacement is @EnableWebSecurity, which now includes the MVC integration automatically.
If you find @EnableWebMvcSecurity in an old project, replace it and move to the SecurityFilterChain bean style used by current Spring Security. The sections below show what it did and how to migrate.
In Spring Security 3.2 it was a convenience annotation placed on a class extending WebSecurityConfigurerAdapter. On top of the plain web security setup, it registered the pieces that connect Spring Security to Spring MVC:
RequestDataValueProcessor that adds the CSRF token to forms rendered by Spring's form tags and Thymeleaf-style view integration.AuthenticationPrincipalArgumentResolver, so a controller method can take the current user with @AuthenticationPrincipal.Spring Security 4.0 moved the MVC integration into @EnableWebSecurity itself. When Spring MVC is on the classpath, enabling web security now registers the same CSRF value processor and argument resolver. Keeping two annotations meant developers had to remember which one to use, so the old one was deprecated and then dropped.
Compiling against a current version gives an error such as cannot find symbol: class EnableWebMvcSecurity. There is no compatibility shim, so you must change the code.
The minimal change is the annotation name. In Spring Security 6 and later, you also have to stop extending WebSecurityConfigurerAdapter, which was removed, and expose a SecurityFilterChain bean instead.
Key differences from the old style:
| Old | Current |
|---|---|
@EnableWebMvcSecurity | @EnableWebSecurity |
extends WebSecurityConfigurerAdapter | A SecurityFilterChain @Bean |
authorizeRequests() | authorizeHttpRequests(...) |
antMatchers, mvcMatchers | requestMatchers |
Chained .and() calls | Lambda DSL with Customizer |
In Spring Boot you can often omit @EnableWebSecurity, since Boot's auto-configuration applies it once spring-boot-starter-security is present. Adding it is still valid and makes the intent explicit.
The MVC integration that @EnableWebMvcSecurity used to provide works out of the box:
CSRF protection is enabled by default for browser-style requests, and form views built with Spring's form tag library include the token automatically.
@EnableWebMvcSecurity with @EnableWebSecurity.WebSecurityConfigurerAdapter and register a SecurityFilterChain bean.authorizeRequests to authorizeHttpRequests and matcher methods to requestMatchers..and() chains with lambda configuration.PasswordEncoder bean such as BCryptPasswordEncoder, and a UserDetailsService bean for users.spring-security-test to confirm access rules still behave the same.No. It was deprecated in Spring Security 4.0 and removed later. Use @EnableWebSecurity on any supported version.
No. @EnableWebSecurity detects Spring MVC on the classpath and registers the CSRF and argument resolver support itself.
Not in Spring Security 6 or later, where the adapter was removed. Declare a SecurityFilterChain bean instead.
Boot's security auto-configuration enables web security by default, so a custom SecurityFilterChain bean is enough. The annotation is optional there.
@EnableWebMvcSecurity belonged to Spring Security 3.2 and was folded into @EnableWebSecurity in version 4.0. To upgrade, swap the annotation, drop WebSecurityConfigurerAdapter, expose a SecurityFilterChain bean and adopt the lambda DSL with authorizeHttpRequests and requestMatchers.