Skip to content

Commit 9055ad2

Browse files
committed
Document Jwt Client Assertion Validation
Closes spring-projectsgh-945
1 parent bd03db3 commit 9055ad2

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

docs/src/docs/asciidoc/configuration-model.adoc

+52
Original file line numberDiff line numberDiff line change
@@ -253,3 +253,55 @@ The supported client authentication methods are `client_secret_basic`, `client_s
253253
* `*AuthenticationManager*` -- An `AuthenticationManager` composed of `JwtClientAssertionAuthenticationProvider`, `ClientSecretAuthenticationProvider`, and `PublicClientAuthenticationProvider`.
254254
* `*AuthenticationSuccessHandler*` -- An internal implementation that associates the "`authenticated`" `OAuth2ClientAuthenticationToken` (current `Authentication`) to the `SecurityContext`.
255255
* `*AuthenticationFailureHandler*` -- An internal implementation that uses the `OAuth2Error` associated with the `OAuth2AuthenticationException` to return the OAuth2 error response.
256+
257+
[[configuring-client-authentication-customizing-jwt-client-assertion-validation]]
258+
=== Customizing Jwt Client Assertion Validation
259+
260+
`JwtClientAssertionDecoderFactory.DEFAULT_JWT_VALIDATOR_FACTORY` is the default factory that provides an `OAuth2TokenValidator<Jwt>` for the specified `RegisteredClient` and is used for validating the `iss`, `sub`, `aud`, `exp` and `nbf` claims of the `Jwt` client assertion.
261+
262+
`JwtClientAssertionDecoderFactory` provides the ability to override the default `Jwt` client assertion validation by supplying a custom factory of type `Function<RegisteredClient, OAuth2TokenValidator<Jwt>>` to `setJwtValidatorFactory()`.
263+
264+
[NOTE]
265+
`JwtClientAssertionDecoderFactory` is the default `JwtDecoderFactory` used by `JwtClientAssertionAuthenticationProvider` that provides a `JwtDecoder` for the specified `RegisteredClient` and is used for authenticating a `Jwt` Bearer Token during OAuth2 client authentication.
266+
267+
A common use case for customizing `JwtClientAssertionDecoderFactory` is to validate additional claims in the `Jwt` client assertion.
268+
269+
The following example shows how to configure `JwtClientAssertionAuthenticationProvider` with a customized `JwtClientAssertionDecoderFactory` that validates an additional claim in the `Jwt` client assertion:
270+
271+
[source,java]
272+
----
273+
@Bean
274+
public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
275+
OAuth2AuthorizationServerConfigurer authorizationServerConfigurer =
276+
new OAuth2AuthorizationServerConfigurer();
277+
http.apply(authorizationServerConfigurer);
278+
279+
authorizationServerConfigurer
280+
.clientAuthentication(clientAuthentication ->
281+
clientAuthentication
282+
.authenticationProviders(configureJwtClientAssertionValidator())
283+
);
284+
285+
return http.build();
286+
}
287+
288+
private Consumer<List<AuthenticationProvider>> configureJwtClientAssertionValidator() {
289+
return (authenticationProviders) ->
290+
authenticationProviders.forEach((authenticationProvider) -> {
291+
if (authenticationProvider instanceof JwtClientAssertionAuthenticationProvider) {
292+
// Customize JwtClientAssertionDecoderFactory
293+
JwtClientAssertionDecoderFactory jwtDecoderFactory = new JwtClientAssertionDecoderFactory();
294+
Function<RegisteredClient, OAuth2TokenValidator<Jwt>> jwtValidatorFactory = (registeredClient) ->
295+
new DelegatingOAuth2TokenValidator<>(
296+
// Use default validators
297+
JwtClientAssertionDecoderFactory.DEFAULT_JWT_VALIDATOR_FACTORY.apply(registeredClient),
298+
// Add custom validator
299+
new JwtClaimValidator<>("claim", "value"::equals));
300+
jwtDecoderFactory.setJwtValidatorFactory(jwtValidatorFactory);
301+
302+
((JwtClientAssertionAuthenticationProvider) authenticationProvider)
303+
.setJwtDecoderFactory(jwtDecoderFactory);
304+
}
305+
});
306+
}
307+
----

0 commit comments

Comments
 (0)