Upgrading the deprecated WebSecurityConfigurerAdapter in Spring Boot 2.7.0
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Spring Boot 2.7.0 marks a significant change in how security configurations are handled. The WebSecurityConfigurerAdapter, which was long used to configure web security, has been deprecated. This shift signals Spring's move towards a more flexible and modular approach to security configuration. Developers must understand how to upgrade old implementations to the new paradigm. This article will guide you through these updates and provide comprehensive examples to illustrate the changes.
Background
WebSecurityConfigurerAdapter was a convenient base class for creating custom security configurations by overriding specific hook methods such as configure(HttpSecurity http) and configure(AuthenticationManagerBuilder auth). However, this approach forced developers into using an inheritance-based model, which can be restrictive and against the modern composition-over-inheritance principle in software design.
Understanding the New Approach
The deprecation of WebSecurityConfigurerAdapter pushes developers towards using simpler, component-based configurations that rely heavily on lambda expressions and builders. The new approach promotes a more explicit and finer-grained security configuration.
Key Components Introduced:
- SecurityFilterChain Configuration:
- Introduced to define a custom filter chain without extending any class.
- Uses
@Beanannotated methods to define security filters.
- Creating a Password Encoder:
- A fresh necessity because the old adapter's convention on password encoding isn’t available.
- UserDetailsService Bean:
- Direct and flexible approach to register user details.
- HttpSecurity Customizer:
- Offers more control and precision with
HttpSecurityconfigurations.
Upgrading Your Security Configuration
Step-by-Step Example of Transitioning
Let's consider an application that initially used WebSecurityConfigurerAdapter.
Upgraded Version:
With the new approach, instead of extending WebSecurityConfigurerAdapter, you'll directly define beans:
Key Differences to Note
| Feature | WebSecurityConfigurerAdapter | New Approach |
| Base Class | Extends WebSecurityConfigurerAdapter | No base class extension |
| Configuration Method | Override configure() | Define SecurityFilterChain Bean |
| Password Encoding | Configured within overridden method | Separate PasswordEncoder Bean |
| Authentication | Customize within adapter | Use AuthenticationManager Bean if needed |
| URI Matching | antMatchers() | requestMatchers() |
| Flexibility | Less modular | Promotes modular configuration |
Best Practices for the New Approach
- Embrace Lambda Expressions: Leverage lambda expressions for concise security configurations.
- Define Beans Explicitly: This shift enforces a clear declaration of components, aligning with Spring Boot's convention-over-configuration philosophy.
- Test Thoroughly: Security configurations might behave differently based on subtle changes. Rigorous testing is essential.
Subtopics
Handling User Details Service
Defining a UserDetailsService is crucial for setting up custom user details:
Customizing Authentication Management
For more complex scenarios where AuthenticationManager needs customization:
Conclusion
Transitioning from WebSecurityConfigurerAdapter involves a structurally different approach, but it provides greater flexibility and aligns with modern software development principles. The new method simplifies security configurations by leveraging Spring's bean management and dependency injection capability while adhering strictly to the separation of concerns.
Adopting these changes early will not only keep your applications secure and up-to-date but will also open up avenues for more flexible security architecture in your applications.

