What Is New in Spring Security 5.3
| Category | Highlights |
|---|---|
| New Features | OAuth2/OIDC success-failure handlers, JwtClaimValidator, ReactiveJwtIssuerAuthenticationManagerResolver, Kotlin DSL extensions (hasRole, custom filter), configurable DefaultAuthenticationEventPublisher, XML namespace support for OAuth2 client/resource-server/login, Opaque Token reactive test support. |
| Improvements | Clock injection for OidcIdTokenValidator, principal configuration on OAuth2AuthorizeRequest, better handling of query-parameter encoding, lazy exception instantiation, accessible color palette for docs, Kotlin DSL marker annotations to prevent scope leaking. |
| Bug Fixes | Typo corrections, AntPathRequestMatcher comment fix, double-escaping of authorize URL parameters, ClassCastException fixes for ServletRequestAttributes, query-parameter double-encoding issues, OAuth2AuthorizationCodeGrant filter matching on query parameters. |
| Dependency Upgrades | Gradle 6.2.2, Kotlin 1.3.70, Spring Boot 2.2.5, spring-build-conventions 0.0.31.RELEASE. |
How does Spring Security 5.3 improve OAuth2/OIDC client and resource-server support?
Spring Security 5.3 adds dedicated success and failure handlers for OAuth2 authorization, a configurable JwtClaimValidator, and a reactive resolver for JWT issuers.
- New
OAuth2AuthorizationRequest.Builderlets you add extra parameters via a consumer, simplifying custom request creation. - Success/failure handlers (
OAuth2AuthorizationSuccessHandler,OAuth2AuthorizationFailureHandler) give you a hook to store state or log events after the authorization flow. JwtClaimValidatorenables fine-grained claim checks without writing a fullJwtDecoderimplementation.- Reactive support:
JwtIssuerReactiveAuthenticationManagerResolverandReactiveJwtIssuerAuthenticationManagerResolverresolve a JWT decoder per issuer at runtime, useful for multi-tenant SaaS. - XML namespace extensions for OAuth2 client, resource server, and login make legacy XML configuration viable again.
http
.oauth2Login()
.authorizationEndpoint()
.authorizationRequestResolver(
request -> OAuth2AuthorizationRequest.from(request)
.additionalParameters(params -> params.put("prompt", "consent"))
.build())
.and()
.successHandler(new MyAuthSuccessHandler())
.failureHandler(new MyAuthFailureHandler());
What Kotlin DSL enhancements are introduced in Spring Security 5.3?
Kotlin DSL now supports role-based authorization, custom filters, and marker annotations that keep the DSL scope clean.
authorizeRequests { hasRole("ADMIN") }mirrors the Java.hasRolemethod, allowing concise role checks.- Custom filter registration:
http.addFilterBefore(myFilter, UsernamePasswordAuthenticationFilter::class.java)can be expressed directly in the DSL. - Marker annotations (
@SecurityDslMarker) prevent accidental leakage of DSL functions into unrelated scopes, reducing compile-time surprises. - Principal name can now be set on
OAuth2AuthorizeRequest.Builder.principal(String), making token acquisition code more readable.
@Bean
fun securityFilterChain(http: HttpSecurity): SecurityFilterChain {
http {
authorizeRequests {
authorize("/admin/**", hasRole("ADMIN"))
authorize(anyRequest, permitAll)
}
oauth2Login { }
addFilterBefore(customFilter, UsernamePasswordAuthenticationFilter::class)
}
return http.build()
}
How can you customize authentication events with the new DefaultAuthenticationEventPublisher?
Spring Security 5.3 makes DefaultAuthenticationEventPublisher configurable via a map and lets you define a default event type.
- Supply a map of
Class<? extends AuthenticationEvent> → ApplicationEventPublisherto route specific authentication events to different listeners. - Use the
setDefaultAuthenticationEventPublishermethod to emit a fallback event when no specific mapping matches. - This flexibility is handy for audit pipelines that need distinct handling for success, failure, and interactive authentication events.
val publisher = DefaultAuthenticationEventPublisher()
publisher.setEventPublishers(
mapOf(
InteractiveAuthenticationSuccessEvent::class.java to successPublisher,
AbstractAuthenticationFailureEvent::class.java to failurePublisher
)
)
publisher.setDefaultAuthenticationEventPublisher(genericPublisher)
What new reactive JWT and opaque-token capabilities are available in Spring Security 5.3?
Reactive support now includes a dedicated resolver for JWT issuers and test utilities for opaque-token introspection.
JwtIssuerReactiveAuthenticationManagerResolverlazily creates aReactiveJwtDecoderper issuer, enabling multi-tenant token validation without restarting the application.- Opaque-token reactive test support (
OpaqueTokenReactiveAuthenticationManagerResolver) simplifies writing WebFlux integration tests for resource servers. - Improved error handling differentiates token-level errors from service-level errors in
NimbusOpaqueTokenIntrospector, giving clearer diagnostics.
@Bean
fun jwtResolver(): ReactiveAuthenticationManagerResolver {
return JwtIssuerReactiveAuthenticationManagerResolver { issuer ->
ReactiveJwtDecoders.fromIssuerLocation(issuer)
}
}
Frequently Asked Questions
Do I need to change my existing OAuth2 client configuration to use the new success/failure handlers?
You can add the handlers without touching the rest of the configuration, simply by calling .successHandler(...) and .failureHandler(...).
Can I still use XML configuration for OAuth2 resource servers?
Yes, Spring Security 5.3 adds XML namespace support for OAuth2 resource server, client, and login.
How do I enable role-based checks in the Kotlin DSL?
Use hasRole("ROLE_NAME") inside authorizeRequests { ... } blocks.
Is the JwtClaimValidator mandatory for JWT validation?
No, it is optional and can be added to the JwtDecoder chain when you need custom claim checks.
What is the simplest way to register a custom filter in a Kotlin DSL configuration?
Call addFilterBefore(myFilter, UsernamePasswordAuthenticationFilter::class) inside the http { ... } block.
How can I test an opaque-token resource server in a WebFlux application?
Use the provided OpaqueTokenReactiveAuthenticationManagerResolver in your test configuration to mock token introspection.