Java 在Nimbus Jose JWT中是否有一种干净的方法来更改默认的JWKSetCache TTL?

Java 在Nimbus Jose JWT中是否有一种干净的方法来更改默认的JWKSetCache TTL?,java,spring-boot,spring-security,Java,Spring Boot,Spring Security,据报道,我们的授权服务器(Auth0)不会更改JWK集(可能永远不会更改?)。为了提高性能,我们希望从远程身份验证服务器缓存密钥的时间比硬编码的默认值(5分钟)长得多,因为需要这些密钥来验证所有请求的令牌,并且每次请求它们都会增加显著的延迟。注意,JWT解码器库类是final 减少对授权服务器的请求数量的阻力最小的路径似乎是使用一个本地路由代理,该路由将处理缓存。有更好的办法吗 版本:Spring Boot 2.2.5(Spring 5.2) //相关导入(省略)来自: //导入org.spri

据报道,我们的授权服务器(Auth0)不会更改JWK集(可能永远不会更改?)。为了提高性能,我们希望从远程身份验证服务器缓存密钥的时间比硬编码的默认值(5分钟)长得多,因为需要这些密钥来验证所有请求的令牌,并且每次请求它们都会增加显著的延迟。注意,JWT解码器库类是
final

减少对授权服务器的请求数量的阻力最小的路径似乎是使用一个本地路由代理,该路由将处理缓存。有更好的办法吗

版本:Spring Boot 2.2.5(Spring 5.2)

//相关导入(省略)来自:
//导入org.springframework.security.oauth2.core
//导入org.springframework.security.oauth2.jwt
@配置
@启用Web安全性
公共类SecurityConfig扩展了WebSecurity配置适配器{
@值(${auth0.accession}”)
私人弦乐观众;
@值(${spring.security.oauth2.resourceserver.jwt.issuer uri}”)
私人字符串发行人;
@凌驾
public void configure(HttpSecurity http)引发异常{
//@formatter:off
http
.会议管理()
.sessionCreationPolicy(sessionCreationPolicy.STATELESS)
.及()
.授权请求()
.antMatchers(“/api/**”).authenticated()
.anyRequest().permitAll()
.及()
.cors()
.及()
.csrf()
.disable()
.oauth2ResourceServer()
.jwt();
//@formatter:on
}
@豆子
JwtDecoder JwtDecoder(){
NimbusJwtDecoder jwtDecoder=(NimbusJwtDecoder)jwtDecoder.fromIdecissuerLocation(发卡机构);
OAuth2TokenValidator audienceValidator=新的audienceValidator(观众);
OAuth2TokenValidator with issuer=JwtValidators.createDefaultWithIssuer(issuer);
OAuth2TokenValidator with audition=新授权授权人2 TokenValidator(with发卡机构、audienceValidator);
jwtDecoder.setJwtValidator(带观众);
返回jwt解码器;
}
}

这个问题再次出现,我最终找到了一个(DefaultJWKSetCache的语法稍有不同)使我能够解决这个问题:

@Bean
JwtDecoder JwtDecoder(){
JWSKeySelector JWSKeySelector;
试一试{
URL jwksUrl=新URL(authConfiguration.getJwksUrl());
JWKSetCache JWKSetCache=新的默认JWKSetCache(authConfiguration.getJwksCacheTtlMins(),TimeUnit.MINUTES);
RemoteJWKSet jwkSet=新的RemoteJWKSet(jwksUrl,null,jwkSetCache);
jwsKeySelector=JWSAlgorithmFamilyJWSKeySelector.fromJWKSource(jwkSet);
}捕获(KeySourceException |格式错误的异常e){
抛出新的InvalidConfigurationException(例如getMessage());
}
DefaultJWTProcessor jwtProcessor=新的DefaultJWTProcessor();
setJWSKeySelector(jwsKeySelector);
NimbusJwtDecoder jwtDecoder=新的NimbusJwtDecoder(jwtProcessor);
OAuth2TokenValidator audienceValidator=新的audienceValidator(authConfiguration.getAudience());
OAuth2TokenValidator with ISSUER=JwtValidators.createDefaultWithIssuer(authConfiguration.getIssuer());
OAuth2TokenValidator with audition=新授权授权人2 TokenValidator(with发卡机构、audienceValidator);
jwtDecoder.setJwtValidator(带观众);
返回jwt解码器;
}

这个问题再次出现,我最终找到了一个(DefaultJWKSetCache的语法稍有不同)使我能够解决这个问题:

@Bean
JwtDecoder JwtDecoder(){
JWSKeySelector JWSKeySelector;
试一试{
URL jwksUrl=新URL(authConfiguration.getJwksUrl());
JWKSetCache JWKSetCache=新的默认JWKSetCache(authConfiguration.getJwksCacheTtlMins(),TimeUnit.MINUTES);
RemoteJWKSet jwkSet=新的RemoteJWKSet(jwksUrl,null,jwkSetCache);
jwsKeySelector=JWSAlgorithmFamilyJWSKeySelector.fromJWKSource(jwkSet);
}捕获(KeySourceException |格式错误的异常e){
抛出新的InvalidConfigurationException(例如getMessage());
}
DefaultJWTProcessor jwtProcessor=新的DefaultJWTProcessor();
setJWSKeySelector(jwsKeySelector);
NimbusJwtDecoder jwtDecoder=新的NimbusJwtDecoder(jwtProcessor);
OAuth2TokenValidator audienceValidator=新的audienceValidator(authConfiguration.getAudience());
OAuth2TokenValidator with ISSUER=JwtValidators.createDefaultWithIssuer(authConfiguration.getIssuer());
OAuth2TokenValidator with audition=新授权授权人2 TokenValidator(with发卡机构、audienceValidator);
jwtDecoder.setJwtValidator(带观众);
返回jwt解码器;
}