Spring Security 7 Upgrade Guide
Spring Security 7 is a major release with meaningful changes for authentication, authorization, OAuth2, SAML, and application configuration. If you are upgrading to Spring Boot 4.2, understanding these changes is essential.
This guide covers the most important Spring Security 7 features, the breaking changes that affect existing applications, and a practical migration path with code examples.
Spring Security 7 Overview
Spring Security 7 is a major release, which means deprecated APIs have been removed and some configuration styles no longer work. The release also introduces new capabilities that simplify modern authentication and authorization.
Major themes:
- Native multi-factor authentication support
- Modular security configuration for servlet and reactive applications
- Stricter and more modern OAuth2 defaults
- Removal of legacy DSL methods
- New password encoders based on Password4j
- Integration of Kerberos and Spring Authorization Server into Spring Security
Spring Security 7 also removes several long-deprecated APIs, including and() in the HttpSecurity DSL, authorizeRequests, MvcRequestMatcher, and AntPathRequestMatcher.
Multi-Factor Authentication
Spring Security 7 adds first-class support for multi-factor authentication. Applications can now require more than one authentication factor without building a custom filter chain from scratch.
Why MFA matters:
- Reduces the risk of credential theft.
- Adds a second layer after password authentication.
- Supports compliance and security requirements.
- Improves protection for administrative and sensitive accounts.
MFA concepts
Spring Security 7 introduces factor-based authorities. After a user completes an additional factor, the application can grant a factor authority that represents the completed factor.
import org.springframework.security.authorization.AuthorityAuthorizationManager;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class SecurityConfig {
@Bean
SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests((authorize) -> authorize
.requestMatchers("/admin/**")
.hasAuthority("FACTOR_AUTHENTICATED")
.anyRequest().authenticated()
)
.formLogin((form) -> form.permitAll());
return http.build();
}
}
The default login page can also display factor information using factor.type and factor.reason parameters, which helps applications guide users through additional authentication steps.
MFA implementation considerations:
- Decide which routes require a second factor.
- Support recovery codes or backup factors.
- Rate-limit factor verification attempts.
- Log factor enrollment and verification events.
- Test session behavior after factor completion.
Modular Configuration
Spring Security 7 supports modular configuration in both servlet and reactive applications. Instead of building one large configuration class, you can organize security concerns into reusable customizers.
Example: modular security configuration
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.SecurityFilterChain;
@Configuration
public class SecurityConfig {
@Bean
SecurityFilterChain apiSecurity(HttpSecurity http) throws Exception {
http
.securityMatcher("/api/**")
.authorizeHttpRequests((authorize) -> authorize
.anyRequest().authenticated()
)
.oauth2ResourceServer((oauth2) -> oauth2.jwt((jwt) -> {}));
return http.build();
}
@Bean
SecurityFilterChain webSecurity(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests((authorize) -> authorize
.requestMatchers("/", "/login").permitAll()
.anyRequest().authenticated()
)
.formLogin((form) -> form.permitAll());
return http.build();
}
}
This approach keeps API security and browser security separate, making each configuration easier to understand and test.
Benefits:
- Clear separation between API and web security.
- Reusable configuration components.
- Easier testing of individual security rules.
- Reduced risk of conflicting matchers.
DSL and API Removals
Spring Security 7 removes several legacy APIs. Applications still using the old style must migrate to the lambda-based DSL.
Removed: and()
The and() method has been removed from the HttpSecurity DSL. Use lambda methods instead.
Before
http
.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin()
.and()
.httpBasic();
After
http
.authorizeHttpRequests((authorize) -> authorize
.anyRequest().authenticated()
)
.formLogin((form) -> form.permitAll())
.httpBasic((basic) -> {});
Removed: authorizeRequests
authorizeRequests has been removed in favor of authorizeHttpRequests.
Before
http.authorizeRequests()
.antMatchers("/public/**").permitAll()
.anyRequest().authenticated();
After
http.authorizeHttpRequests((authorize) -> authorize
.requestMatchers("/public/**").permitAll()
.anyRequest().authenticated()
);
Removed request matchers
MvcRequestMatcher and AntPathRequestMatcher have been removed in favor of PathPatternRequestMatcher.
import org.springframework.security.web.servlet.util.matcher.PathPatternRequestMatcher;
PathPatternRequestMatcher matcher =
PathPatternRequestMatcher.withDefaults()
.matcher("/api/**");
Removed AuthorizationManager#check
AuthorizationManager#check has been removed in favor of AuthorizationManager#authorize.
OAuth2 Changes
Spring Security 7 makes several important OAuth2 changes. Some are security improvements, while others are breaking changes.
Password grant removed
Support for the OAuth2 password grant has been removed. The password grant is discouraged by the OAuth 2.0 specification because it exposes user credentials directly to the client.
Use the authorization code flow with PKCE for user-facing applications, and client credentials for service-to-service communication.
PKCE enabled by default
PKCE is now enabled by default in the OAuth 2.0 Authorization Server. This improves security for public clients and mobile applications.
Dynamic client registration
Spring Security 7 adds support for the OAuth 2.0 Dynamic Registration Protocol. This allows clients to register dynamically instead of requiring manual registration for every client.
OAuth2 support for HTTP service clients
Spring Security 7 adds OAuth2 support for HTTP service clients, making it easier to call protected services with OAuth2 credentials.
Type-level ClientRegistrationId
@ClientRegistrationId can now be applied at the type level, removing the need to repeat it on every method.
@ClientRegistrationId("github")
public class GitHubClient {
public UserProfile getUser() {
// Call GitHub API
return null;
}
}
New Password Encoders
Spring Security 7 adds Password4j-based password encoders for popular hashing algorithms. These provide additional options for teams with specific compliance or interoperability requirements.
Available encoders
Argon2Password4jPasswordEncoderBcryptPassword4jPasswordEncoderScryptPassword4jPasswordEncoderPbkdf2Password4jPasswordEncoderBalloonHashingPassword4jPasswordEncoder
Example: BCrypt with Password4j
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.crypto.password4j.BcryptPassword4jPasswordEncoder;
@Configuration
public class PasswordConfig {
@Bean
PasswordEncoder passwordEncoder() {
return new BcryptPassword4jPasswordEncoder();
}
}
Password encoder best practices:
- Use BCrypt, Argon2, SCrypt, or PBKDF2 for password hashing.
- Never store plaintext passwords.
- Use a unique salt per password.
- Rehash passwords when encoder parameters change.
- Test login performance with realistic concurrency.
SAML 2.0 Changes
Spring Security 7 includes several SAML 2.0 changes that may affect existing integrations.
OpenSAML 5 required
Support for OpenSAML 4 has been removed. Applications must migrate to OpenSAML 5.
AssertingPartyMetadata
API methods based on AssertingPartyDetails have been removed in favor of the AssertingPartyMetadata interface.
GET request support removed
GET request support has been removed from Saml2AuthenticationTokenConverter. Review your SAML login flows and ensure they use supported request methods.
JDBC metadata repository
Spring Security 7 adds a JDBC-based AssertingPartyMetadataRepository, which is useful for applications that manage multiple SAML relying parties.
Migration Checklist
- Upgrade to the latest Spring Security 6.5 patch release before moving to 7.
- Review compiler warnings and remove deprecated API usage.
- Replace
and()with lambda-based DSL methods. - Replace
authorizeRequestswithauthorizeHttpRequests. - Replace
MvcRequestMatcherandAntPathRequestMatcherwithPathPatternRequestMatcher. - Remove OAuth2 password grant usage.
- Migrate SAML integrations to OpenSAML 5.
- Review password encoders and rehash strategy.
- Test MFA flows, session behavior, and logout.
- Run security integration tests and penetration tests.
Search for removed APIs
authorizeRequests
.and()
MvcRequestMatcher
AntPathRequestMatcher
AuthorizationManager#check
Update the Spring Boot version
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>4.2.0</version>
<relativePath/>
</parent>
FAQs
What is new in Spring Security 7?
Spring Security 7 adds multi-factor authentication, modular configuration, OAuth2 dynamic client registration, PKCE by default, Password4j-based encoders, and several API improvements.
Is the and() method removed in Spring Security 7?
Yes. and() has been removed from the HttpSecurity DSL. Use lambda-based configuration methods instead.
Is authorizeRequests removed?
Yes. Use authorizeHttpRequests instead.
Is the OAuth2 password grant still supported?
No. Password grant support has been removed. Use authorization code with PKCE or client credentials instead.
Do I need OpenSAML 5?
Yes. Spring Security 7 removes OpenSAML 4 support, so SAML integrations must migrate to OpenSAML 5.
Can I still use AntPathRequestMatcher?
No. It has been removed in favor of PathPatternRequestMatcher.
Should I upgrade directly from Spring Security 6 to 7?
It is safer to upgrade to the latest Spring Security 6.5 patch first, remove deprecated API usage, and then move to Spring Security 7.
Does Spring Security 7 require Spring Boot 4?
Spring Security 7 is aligned with the Spring Boot 4 generation. Use the Spring Boot dependency management version to keep Spring Security and Spring Framework compatible.
Final Thoughts
Spring Security 7 is a significant upgrade that modernizes configuration, strengthens OAuth2 defaults, and adds native MFA support. The main migration work involves removing legacy DSL methods, replacing old request matchers, and updating OAuth2 and SAML integrations.
Upgrade incrementally, rely on compiler warnings and tests, and validate authentication flows in staging before production rollout.
Comments
Post a Comment