Skip to content

Commit 93b8c65

Browse files
committed
Document JwtClaimValidator
Fixes gh-8076
1 parent 6db921c commit 93b8c65

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

docs/manual/src/docs/asciidoc/_includes/about/whats-new.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Here's what you'll see in this release:
3636
* OAuth 2.0 Resource Server
3737
** Added support for <<oauth2resourceserver-multitenancy,multiple issuers>>
3838
** Added <<testing-opaque-token,test support for Opaque Tokens>>
39-
** Added https://github.com/spring-projects/spring-security/pull/7962[generic claim validator]
39+
** Added <<oauth2resourceserver-jwt-validation-custom,generic claim validator>>
4040
** Added https://github.com/spring-projects/spring-security/issues/5185[XML support]
4141
** Improved https://github.com/spring-projects/spring-security/pull/7826[bearer token error handling] for JWT and Opaque Token
4242
* SAML 2.0

docs/manual/src/docs/asciidoc/_includes/servlet/oauth2/oauth2-resourceserver.adoc

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -695,9 +695,19 @@ Adding a check for the `aud` claim is simple with the `OAuth2TokenValidator` API
695695

696696
[source,java]
697697
----
698-
public class AudienceValidator implements OAuth2TokenValidator<Jwt> {
699-
OAuth2Error error = new OAuth2Error("invalid_token", "The required audience is missing", null);
698+
OAuth2TokenValidator<Jwt> audienceValidator() {
699+
return new JwtClaimValidator<List<String>>(AUD, aud -> aud.contains("messaging"));
700+
}
701+
----
702+
703+
Or, for more control you can implement your own `OAuth2TokenValidator`:
704+
705+
[source,java]
706+
----
707+
static class AudienceValidator implements OAuth2TokenValidator<Jwt> {
708+
OAuth2Error error = new OAuth2Error("custom_code", "Custom error message", null);
700709
710+
@Override
701711
public OAuth2TokenValidatorResult validate(Jwt jwt) {
702712
if (jwt.getAudience().contains("messaging")) {
703713
return OAuth2TokenValidatorResult.success();
@@ -706,6 +716,12 @@ public class AudienceValidator implements OAuth2TokenValidator<Jwt> {
706716
}
707717
}
708718
}
719+
720+
// ...
721+
722+
OAuth2TokenValidator<Jwt> audienceValidator() {
723+
return new AudienceValidator();
724+
}
709725
----
710726

711727
Then, to add into a resource server, it's a matter of specifying the `JwtDecoder` instance:
@@ -717,7 +733,7 @@ JwtDecoder jwtDecoder() {
717733
NimbusJwtDecoder jwtDecoder = (NimbusJwtDecoder)
718734
JwtDecoders.fromIssuerLocation(issuerUri);
719735
720-
OAuth2TokenValidator<Jwt> audienceValidator = new AudienceValidator();
736+
OAuth2TokenValidator<Jwt> audienceValidator = audienceValidator();
721737
OAuth2TokenValidator<Jwt> withIssuer = JwtValidators.createDefaultWithIssuer(issuerUri);
722738
OAuth2TokenValidator<Jwt> withAudience = new DelegatingOAuth2TokenValidator<>(withIssuer, audienceValidator);
723739

0 commit comments

Comments
 (0)