@@ -695,9 +695,19 @@ Adding a check for the `aud` claim is simple with the `OAuth2TokenValidator` API
695
695
696
696
[source,java]
697
697
----
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);
700
709
710
+ @Override
701
711
public OAuth2TokenValidatorResult validate(Jwt jwt) {
702
712
if (jwt.getAudience().contains("messaging")) {
703
713
return OAuth2TokenValidatorResult.success();
@@ -706,6 +716,12 @@ public class AudienceValidator implements OAuth2TokenValidator<Jwt> {
706
716
}
707
717
}
708
718
}
719
+
720
+ // ...
721
+
722
+ OAuth2TokenValidator<Jwt> audienceValidator() {
723
+ return new AudienceValidator();
724
+ }
709
725
----
710
726
711
727
Then, to add into a resource server, it's a matter of specifying the `JwtDecoder` instance:
@@ -717,7 +733,7 @@ JwtDecoder jwtDecoder() {
717
733
NimbusJwtDecoder jwtDecoder = (NimbusJwtDecoder)
718
734
JwtDecoders.fromIssuerLocation(issuerUri);
719
735
720
- OAuth2TokenValidator<Jwt> audienceValidator = new AudienceValidator ();
736
+ OAuth2TokenValidator<Jwt> audienceValidator = audienceValidator ();
721
737
OAuth2TokenValidator<Jwt> withIssuer = JwtValidators.createDefaultWithIssuer(issuerUri);
722
738
OAuth2TokenValidator<Jwt> withAudience = new DelegatingOAuth2TokenValidator<>(withIssuer, audienceValidator);
723
739
0 commit comments