Migrate WebSecurityConfigurerAdapter to Security Beans
Migrate WebSecurityConfigurerAdapter to SecurityFilterChain, WebSecurityCustomizer and AuthenticationManager beans, with before and after code and pitfalls.

Spring Security deprecated the adapter in 5.7 and removed it in 6.0, so any project moving to Spring Boot 3 or 4 has to migrate. The new model is component based: you declare beans, not override methods.
Before
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/public/**").permitAll()
.anyRequest().authenticated()
.and().formLogin();
}
@Override
public void configure(WebSecurity web) {
web.ignoring().antMatchers("/static/**");
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
}
}After
@Configuration
@EnableWebSecurity
public class SecurityConfig {
@Bean
SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.authorizeHttpRequests(auth -> auth
.requestMatchers("/public/**", "/static/**").permitAll()
.anyRequest().authenticated())
.formLogin(Customizer.withDefaults());
return http.build();
}
@Bean
PasswordEncoder passwordEncoder() {
return PasswordEncoderFactories.createDelegatingPasswordEncoder();
}
}A UserDetailsService bean is picked up automatically, so configure(AuthenticationManagerBuilder) often disappears completely. See a custom UserDetailsService with JPA.
Mapping each override
| Old override | New |
|---|---|
configure(HttpSecurity) | SecurityFilterChain bean |
configure(WebSecurity) ignoring | WebSecurityCustomizer bean, or permitAll in the chain |
configure(AuthenticationManagerBuilder) | UserDetailsService bean, or AuthenticationManager bean |
authenticationManagerBean() | inject AuthenticationConfiguration |
Ignoring paths
@Bean
WebSecurityCustomizer ignoreStatic() {
return web -> web.ignoring().requestMatchers("/static/**");
}Ignored requests skip the whole filter chain, including security headers. Spring now recommends permitAll() in the chain for most cases, as in the example above, so headers and logging still apply.
Exposing an AuthenticationManager
@Bean
AuthenticationManager authenticationManager(AuthenticationConfiguration config) throws Exception {
return config.getAuthenticationManager();
}Use this where a controller authenticates manually, for example a token login endpoint.
Pitfalls
- Matcher semantics.
antMatchersandmvcMatchersare replaced byrequestMatchers, which picks the right matcher for the app. Re-test every path rule; trailing slash behaviour can differ. - **Multiple adapters with
@Order.** Each becomes aSecurityFilterChainbean with@Orderand asecurityMatcher. - Global method security.
@EnableGlobalMethodSecurityis replaced by@EnableMethodSecurity. - Default behaviour. Some defaults changed, such as requiring explicit saving of the
SecurityContextin custom filters.
Test the migration
Write tests that assert the status for each path and role before you start, and run them after:
mockMvc.perform(get("/admin")).andExpect(status().isUnauthorized());
mockMvc.perform(get("/admin").with(user("u").roles("USER"))).andExpect(status().isForbidden());Passing before and after is the proof. The related DSL change is in the lambda DSL.
A note on testing
Security migrations fail silently: a rule that no longer matches leaves a path open or closed. Keep a table of every path, method and role pair the application serves, and turn it into a parameterised test. Running it before the change records the truth, and running it after proves nothing moved. Add the paths that the adapter handled implicitly, such as the login page, the logout URL and static resources, since the new chain only protects what you declare. See also configuring a SecurityFilterChain and Spring Security 7.
The shape change in one paragraph
In the old model, your class extended an adapter and overrode methods that the framework called to configure security. In the new model you declare beans, and the framework picks them up. A SecurityFilterChain bean replaces configure(HttpSecurity), a WebSecurityCustomizer bean replaces configure(WebSecurity), a UserDetailsService bean replaces the AuthenticationManagerBuilder setup, and an AuthenticationManager bean is exposed from AuthenticationConfiguration when you need one. Seeing the four bean types makes the rest mechanical, and it explains why a migrated class is often shorter than the original.
Several chains for several kinds of request
The bean model allows more than one SecurityFilterChain, each with a securityMatcher and an @Order. That fits an application with an API under /api/** secured by tokens and a UI secured by a session and a login form. Give the API chain the lower order number and a matcher for its paths, and let the other chain catch everything else. In the adapter model you needed two inner classes with @Order to do the same, and this version is easier to read. Remember that the first chain whose matcher fits handles the request alone, so a path covered by neither rule set is open or closed depending on the last chain's anyRequest().
Behavior that changed along the way
Migration is more than renaming. Several defaults changed between Spring Security 5 and 6, and they are what make a "working" migrated configuration behave differently. authorizeRequests became authorizeHttpRequests, with requestMatchers replacing antMatchers and mvcMatchers. Authorization is now applied to every dispatch type, including error and forward dispatches, so a page that shows an error may need permitAll on /error. CSRF protection is deferred, which affects single page applications that read the token early. Check each of these in a test that logs in and out, posts a form and requests the error page.
Proving that the security is the same
Write the tests before you migrate, against the old configuration, and keep them unchanged afterwards. A table of paths and roles with the expected status for each, run through MockMvc with @WithMockUser, takes an hour and gives you proof that nothing opened by accident. A configuration that migrates cleanly and lets anonymous users into /admin is the failure to guard against, and only a test finds it.
A table of old calls and new calls
Most of the migration is looking things up. http.authorizeRequests() becomes http.authorizeHttpRequests(...). antMatchers("/admin/**") becomes requestMatchers("/admin/**"). .and().formLogin() becomes a separate .formLogin(Customizer.withDefaults()). http.csrf().disable() becomes http.csrf(csrf -> csrf.disable()), and you should reconsider whether disabling it is right, since an application with cookie sessions needs CSRF protection. http.cors() becomes http.cors(Customizer.withDefaults()), which reads a CorsConfigurationSource bean. @EnableGlobalMethodSecurity becomes @EnableMethodSecurity. Keep this table in the pull request description so reviewers can check each line.
The lambda style is the point
The new DSL passes a lambda to each section instead of chaining with and(), which makes the structure visible: each section is a block, and a block can be moved or extracted into a method without breaking a chain. A long configuration reads better if each concern, such as login, headers and sessions, is a small private method taking HttpSecurity. Recent versions have removed and() entirely, so migrating to lambdas also prepares you for the next upgrade.
Ignoring paths without opening a hole
The WebSecurityCustomizer with web.ignoring() removes security entirely for a path, including the security headers. Use it only for static resources that need no protection. For anything that is merely public, such as the login page or a health check, keep it in the filter chain with permitAll, so the headers and the logging still apply and the path is visible in your security configuration. Moving a path from ignoring to permitAll is a common review comment in migrations, and a worthwhile one.
When this does not apply
Projects on Spring Boot 2.x still on Security 5.6 or earlier keep the adapter until the framework upgrade. Reference: the Spring blog post on the adapter removal and the Spring Security migration guide.


