For the latest stable version, please use Spring Security 6.4.1!spring-doc.cn

What’s New in Spring Security 6.3

Spring Security 6.3 provides a number of new features. Below are the highlights of the release, or you can view the release notes for a detailed listing of each feature and bug fix.spring-doc.cn

Passive JDK Serialization Support

When it comes to its support for JDK-serialized security components, Spring Security has historically been quite aggressive, supporting each serialization version for only one Spring Security minor version. This meant that if you had JDK-serialized security components, then they would need to be evacuated before upgrading to the next Spring Security version since they would no longer be deserializable.spring-doc.cn

Now that Spring Security performs a minor release every six months, this became a much larger pain point. To address that, Spring Security now will maintain passivity with JDK serialization, like it does with JSON serialization, making for more seamless upgrades.spring-doc.cn

Authorization

An ongoing theme for the last several releases has been to refactor and improve Spring Security’s authorization subsystem. Starting with replacing the AccessDecisionManager API with AuthorizationManager it’s now come to the point where we are able to add several exciting new features.spring-doc.cn

Annotation Parameters - #14480

The first 6.3 feature is support for annotation parameters. Consider Spring Security’s support for meta-annotations like this one:spring-doc.cn

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@PreAuthorize("hasAuthority('SCOPE_message:read')")
public @interface HasMessageRead {}
Kotlin
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@PreAuthorize("hasAuthority('SCOPE_message:read')")
annotation class HasMessageRead

Before this release, something like this is only helpful when it is used widely across the codebase. But now, you can add parameters like so:spring-doc.cn

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@PreAuthorize("hasAuthority('SCOPE_{scope}')")
public @interface HasScope {
	String scope();
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@PreAuthorize("hasAuthority('SCOPE_{scope}')")
annotation class HasScope (val scope:String)

making it possible to do things like this:spring-doc.cn

@HasScope("message:read")
public String method() { ... }
@HasScope("message:read")
fun method(): String { ... }

and apply your SpEL expression in several more places.spring-doc.cn

Secure Return Values - #14596, #14597

Since the early days of Spring Security, you’ve been able to annotate Spring beans with @PreAuthorize and @PostAuthorize. But controllers, services, and repositories are not the only things you care to secure. For example, what about a domain object Order where only admins should be able to call the Order#getPayment method?spring-doc.cn

Now in 6.3, you can annotate those methods, too. First, annotate the getPayment method like you would a Spring bean:spring-doc.cn

public class Order {

	@HasScope("payment:read")
	Payment getPayment() { ... }

}
class Order {

	@HasScope("payment:read")
	fun getPayment(): Payment { ... }

}
public interface OrderRepository implements CrudRepository<Order, String> {

	@AuthorizeReturnObject
	Optional<Order> findOrderById(String id);

}
interface OrderRepository : CrudRepository<Order, String> {
    @AuthorizeReturnObject
    fun findOrderById(id: String?): Optional<Order?>?
}

At that point, Spring Security will protect any Order returned from findOrderById by way of proxying the Order instance.spring-doc.cn

Error Handling - #14598, #14600, #14601

In this release, you can also intercept and handle failure at the method level with its last new method security annotation.spring-doc.cn

public class Payment {
    @HandleAuthorizationDenied(handlerClass=Mask.class)
    @PreAuthorize("hasAuthority('card:read')")
    public String getCreditCardNumber() { ... }
}
class Payment {
    @HandleAuthorizationDenied(handlerClass=Mask.class)
    @PreAuthorize("hasAuthority('card:read')")
    fun getCreditCardNumber(): String { ... }
}

and publish a Mask bean:spring-doc.cn

@Component
public class Mask implements MethodAuthorizationDeniedHandler {
	@Override
    public Object handleDeniedInvocation(MethodInvocation invocation, AuthorizationResult result) {
		return "***";
    }
}
@Component
class Mask : MethodAuthorizationDeniedHandler {
    fun handleDeniedInvocation(invocation: MethodInvocation?, result: AuthorizationResult?): Any = "***"
}

then any unauthorized call to Payment#getCreditCardNumber will return *** instead of the number.spring-doc.cn

You can see all these features at work together in the latest Spring Security Data sample.spring-doc.cn

Compromised Password Checking - #7395

If you are going to let users pick passwords, it’s critical to ensure that such a password isn’t already compromised. Spring Security 6.3 makes this as simple as publishing a CompromisedPasswordChecker bean:spring-doc.cn

@Bean
public CompromisedPasswordChecker compromisedPasswordChecker() {
    return new HaveIBeenPwnedRestApiPasswordChecker();
}
@Bean
fun compromisedPasswordChecker(): CompromisedPasswordChecker = HaveIBeenPwnedRestApiPasswordChecker()

spring-security-rsa is now part of Spring Security - #14202

Since 2017, Spring Security has been undergoing a long-standing initiative to fold various Spring Security extensions into Spring Security proper. In 6.3, spring-security-rsa becomes the latest of these projects which will help the team maintain and add features to it, long-term.spring-doc.cn

OAuth 2.0 Token Exchange Grant - #5199

One of the most highly-voted OAuth 2.0 features in Spring Security is now in place in 6.3, which is the support for the OAuth 2.0 Token Exchange grant.spring-doc.cn

For any client configured for token exchange, you can activate it in Spring Security by adding a TokenExchangeAuthorizedClientProvider instance to your OAuth2AuthorizedClientManager like so:spring-doc.cn

@Bean
public OAuth2AuthorizedClientProvider tokenExchange() {
	return new TokenExchangeOAuth2AuthorizedClientProvider();
}
@Bean
fun tokenExchange(): OAuth2AuthorizedClientProvider = TokenExchangeOAuth2AuthorizedClientProvider()

and then use the @RegisteredOAuth2AuthorizedClient annotation as per usual to retrieve the appropriate token with the expanded privileges your resource server needs.spring-doc.cn

Additional Highlights

And for an exhaustive list, please see the release notes for 6.3.0-RC1, 6.3.0-M3, 6.3.0-M2, and 6.3.0-M1.spring-doc.cn