Spring Boot Security is a powerful framework for securing web applications. However, upgrading to the latest versions (Spring Boot 3+ and Spring Security 6.1+) introduces breaking changes, deprecations, and new patterns that require refactoring.
In this guide, we will:
โ
Debug common Spring Security upgrade issues
โ
Upgrade to the latest Spring Boot & Security versions
โ
Implement state-of-the-art best practices
๐ Common Debugging Issues After Upgrading Spring Security
Upgrading from Spring Boot 2.x โ 3.x and Spring Security 5 โ 6+ often results in errors like:
- Deprecated Methods
The method cors() from the type HttpSecurity has been deprecated since version 6.1 and marked for removalโ Fix: Use the new Lambda DSL syntax.
- Functional Interface Errors
The target type of this expression must be a functional interfaceโ Fix: Use the correct method references in security configurations.
- Missing Beans (
AuthenticationManager&SecurityFilterChain)Consider defining a bean of type 'org.springframework.security.authentication.AuthenticationManager'โ Fix: Explicitly define
AuthenticationManagerandSecurityFilterChain. - CORS Issues (Blocked Requests in Frontend)
Access to fetch at 'http://localhost:8080/api' from origin 'http://localhost:4200' has been blocked by CORS policyโ Fix: Ensure proper CORS configuration with
CorsConfigurationSource.
๐ ๏ธ How to Upgrade to Spring Security 6+ in Spring Boot 3+
1๏ธโฃ Update Dependencies
Modify pom.xml (Maven) to use the latest versions:
<dependencies>
<!-- Spring Boot Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Security -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- JWT Token Authentication -->
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.11.5</version>
</dependency>
<!-- Password Encryption -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-crypto</artifactId>
</dependency>
</dependencies>
๐ Why?
- Spring Boot 3+ uses Jakarta EE, replacing
javax.servletwithjakarta.servlet. - Some Spring Security classes have changed.
2๏ธโฃ Convert WebSecurityConfigurerAdapter to SecurityFilterChain
In Spring Security 6+, WebSecurityConfigurerAdapter is removed. Instead, define security as a SecurityFilterChain bean.
โ Old Approach (Deprecated)
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/public/**").permitAll()
.anyRequest().authenticated()
.and()
.httpBasic();
}
}
โ New Approach (Spring Security 6)
@Configuration
public class SecurityConfig {
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.csrf(csrf -> csrf.disable())
.authorizeHttpRequests(auth -> auth
.requestMatchers("/public/**").permitAll()
.anyRequest().authenticated()
)
.httpBasic(Customizer.withDefaults());
return http.build();
}
}
๐ Why?
authorizeRequests()โ replaced withauthorizeHttpRequests().antMatchers()โ replaced withrequestMatchers()..and()โ not required in lambda-based DSL.
3๏ธโฃ Define AuthenticationManager as a Bean
Spring Security 6 no longer auto-configures AuthenticationManager. You must define it manually.
โ Fixed Approach
@Bean
public AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration) throws Exception {
return authenticationConfiguration.getAuthenticationManager();
}
4๏ธโฃ Implement Proper CORS Configuration
CORS issues are common in frontend-backend integrations (e.g., Angular, React).
โ
Best Practice: Using CorsConfigurationSource
@Bean
public CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowedOrigins(List.of("http://localhost:4200")); // Replace with frontend origin
configuration.setAllowedMethods(List.of("GET", "POST", "PUT", "DELETE"));
configuration.setAllowCredentials(true);
configuration.setAllowedHeaders(List.of("Authorization", "Cache-Control", "Content-Type"));
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration);
return source;
}
5๏ธโฃ Implement JWT-Based Authentication (Modern Approach)
Instead of storing sessions, use JWT (JSON Web Token) authentication.
Generate JWT Token
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import java.util.Date;
@Service
public class JwtUtil {
private static final String SECRET_KEY = "your-secret-key";
public String generateToken(String username) {
return Jwts.builder()
.setSubject(username)
.setIssuedAt(new Date())
.setExpiration(new Date(System.currentTimeMillis() + 1000 * 60 * 60 * 10))
.signWith(SignatureAlgorithm.HS256, SECRET_KEY)
.compact();
}
}
Validate JWT Token
public boolean validateToken(String token) {
try {
Jwts.parser().setSigningKey(SECRET_KEY).parseClaimsJws(token);
return true;
} catch (Exception e) {
return false;
}
}
๐ Why?
- JWT eliminates the need for server-side session management.
- Stateless authentication is faster and more scalable.
๐ฅ Best Practices for Spring Boot Security
- โ
Use
SecurityFilterChaininstead ofWebSecurityConfigurerAdapter. - โ Disable CSRF only when using JWT authentication.
- โ
Use
requestMatchers()instead ofantMatchers(). - โ
Always encrypt passwords with
BCryptPasswordEncoder. - โ Use JWT instead of traditional sessions.
- โ Ensure CORS is properly configured for frontend-backend integration.
- โ Store sensitive config values (secrets, API keys) in environment variables.
- โ Keep dependencies up to date to avoid security vulnerabilities.
๐ฏ Final Thoughts
Upgrading to Spring Boot 3+ and Spring Security 6+ brings performance improvements, better security, and modern programming patterns. By migrating from WebSecurityConfigurerAdapter to SecurityFilterChain, fixing CORS, and implementing JWT authentication, you ensure your application is secure, scalable, and up-to-date.
โ
Need Help?
Drop your questions below! ๐
Heya i’m for the primary time here. I found this board and I to
find It truly helpful & it helped me out a lot.
I’m hoping to offer one thing back and help others such as you aided me.
Thank you for your comment! If you need to get in touch, you can reach us at:
Phone: +213-555947422
Email: one@sowft.com
Follow us on social media:
Follow us on Facebook | Follow us on LinkedIn
Hi, I do believe this is an excellent website. I stumbledupon it
๐ I may come back once again since i have book marked it.
Money and freedom is the best way to change, may
you be rich and continue to help other people.
Thank you for your comment! If you need to get in touch, you can reach us at:
Phone: +213-555947422
Email: one@sowft.com
Follow us on social media:
Follow us on Facebook | Follow us on LinkedIn
Very descriptive article, I loved that a lot. Will there be a part 2?
Thank you for your comment! If you need to get in touch, you can reach us at:
Phone: +213-555947422
Email: one@sowft.com
Follow us on social media:
Follow us on Facebook | Follow us on LinkedIn
Heya exceptional blog! Does running a blog like this take a large amount
of work? I have no knowledge of coding however I had been hoping to start my own blog soon. Anyway, if you have any ideas or techniques for new
blog owners please share. I know this is off topic nevertheless I simply
needed to ask. Thanks a lot!
Thank you for your comment! If you need to get in touch, you can reach us at:
Phone: +213-555947422
Email: one@sowft.com
Follow us on social media:
Follow us on Facebook | Follow us on LinkedIn
Does your website have a contact page? I’m having trouble locating it
but, I’d like to shoot you an e-mail. I’ve got some ideas for your blog you might be interested in hearing.
Either way, great blog and I look forward to seeing it grow
over time.
Thank you for your comment! If you need to get in touch, you can reach us at:
Phone: +213-555947422
Email: one@sowft.com
Follow us on social media:
Follow us on Facebook | Follow us on LinkedIn