spring-securityspring-test-mvc

WebMvcTest: Migrating from WebSecurityConfigurerAdapter to SecurityFilterChain


As described here Spring Security deprecated WebSecurityConfigurerAdapter which I've been using for some time. I used the component based approach and introduced SecurityFilterChain and InMemoryUserDetailsManager beans (see: commit) but then one of my tests, which is using @WithMockUser failed.

Does @WebMvcTest tests work with @WithMockUser when using Spring Security component based approach (SecurityFilterChain)?

Whole project, with failing test, is located: https://github.com/pszemus/spring-security-test


Solution

  • You should add @Import(YourSecurityConfiguration.class) in your test class. The @WebMvcTest is not picking up the configuration automatically, so you have to tell it explicitly which configuration to use.