feat(ap1): enforce resource audience
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
package com.example.keycloakpattern;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.oauth2.core.DelegatingOAuth2TokenValidator;
|
||||
import org.springframework.security.oauth2.core.OAuth2TokenValidator;
|
||||
import org.springframework.security.oauth2.jwt.Jwt;
|
||||
import org.springframework.security.oauth2.jwt.JwtDecoder;
|
||||
import org.springframework.security.oauth2.jwt.JwtValidators;
|
||||
import org.springframework.security.oauth2.jwt.NimbusJwtDecoder;
|
||||
|
||||
@Configuration
|
||||
public class JwtDecoderConfig {
|
||||
|
||||
@Bean
|
||||
JwtDecoder jwtDecoder(
|
||||
@Value("${spring.security.oauth2.resourceserver.jwt.issuer-uri}") String issuer,
|
||||
@Value("${spring.security.oauth2.resourceserver.jwt.jwk-set-uri}") String jwkSetUri,
|
||||
@Value("${security.expected-audience}") String expectedAudience
|
||||
) {
|
||||
NimbusJwtDecoder decoder = NimbusJwtDecoder.withJwkSetUri(jwkSetUri).build();
|
||||
OAuth2TokenValidator<Jwt> issuerAndTimestamp =
|
||||
JwtValidators.createDefaultWithIssuer(issuer);
|
||||
OAuth2TokenValidator<Jwt> audience = new AudienceValidator(expectedAudience);
|
||||
decoder.setJwtValidator(
|
||||
new DelegatingOAuth2TokenValidator<>(issuerAndTimestamp, audience)
|
||||
);
|
||||
return decoder;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user