Spring / Spring Security
Difference between Spring Security 5 and 6.
Spring Security 6 (shipped with Spring Boot 3) represents a significant modernization of the framework, removing long-deprecated APIs and enforcing more secure defaults.
1. Fundamental Baseline Changes
- Java Version: Minimum requirement moved from Java 8/11 to Java 17.
- Jakarta Migration: All
javax.*imports (Servlets, Persistence) have been replaced byjakarta.*.
2. Configuration Overhaul
The most noticeable change for developers is the removal of the old configuration style.
| Feature | Spring Security 5 | Spring Security 6 |
|---|---|---|
| Setup Method | Extending WebSecurityConfigurerAdapter |
Component-based (Bean-based) configuration |
| Request Matchers | antMatchers(), mvcMatchers() |
Unified requestMatchers() |
| HTTP Authorization | authorizeRequests() |
authorizeHttpRequests() |
| Method Security | @EnableGlobalMethodSecurity |
@EnableMethodSecurity |
3. Lambda DSL Requirement
Spring Security 6 forces the use of the Lambda DSL to make configurations more readable and less prone to order-dependent errors.
Spring Security 6 Example:
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.csrf(csrf -> csrf.disable())
.authorizeHttpRequests(auth -> auth
.requestMatchers("/api/public/**").permitAll()
.anyRequest().authenticated()
);
return http.build();
}
4. Security Hardening & Observability
- Default Denial: In version 6, if a request does not match an explicit rule, it is denied by default (Defense-in-depth).
- Lazy CSRF Tokens: CSRF tokens are no longer loaded on every request by default, which is better for performance in stateless applications.
- Micrometer Support: Integrated observability for tracking security metrics and traces.
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
