首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SpringSecurity5.2 --如何自定义由NimbusJWTDecoder使用的OAuth2ResourceServer?

SpringSecurity5.2 --如何自定义由NimbusJWTDecoder使用的OAuth2ResourceServer?
EN

Stack Overflow用户
提问于 2020-01-15 03:32:40
回答 1查看 4.3K关注 0票数 0

我有一个openid提供者(openam)在本地运行。我使用的是一个自签名证书,jwks url是@ https://localhost:8443/openam/oauth2/connect/

由于ssl证书是自签名的,所以在解码oidc令牌时,我将得到一个SSLHandshake异常。我尝试使用自定义rest模板,方法是创建一个自定义JwtDecoder (如https://docs.spring.io/spring-security/site/docs/current/reference/html5/#oauth2resourceserver-jwt-decoder-dsl中所建议的)

代码语言:javascript
复制
@Bean
public JwtDecoder jwtDecoder() {
NimbusJwtDecoder.withJwkSetUri("https://localhost:8443/openam/oauth2/connect").restOperations(myCustomRestTemplateThatAllowsSelfSignedCerts()).build();
}

但是这个解码器似乎没有被使用。正在使用OidcIdTokenDecoderFactory来创建解码器。这个类似乎不允许我们传递自定义的jwtDecoder。

为什么oauthResourceServer().jwt().decoder(customDecoder())不能工作?如何让解码器使用具有自签名证书的网站jwks uri?

我正在考虑的一个选项是将自签名证书添加到我的jdk的仙人掌文件夹中。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-15 17:04:49

OAuth2LoginAuthenticationFilter正在调用OidcAuthorizationCodeAuthenticationProvider进行OpenID身份验证。要更改它使用的JwtDecoder,您应该有一个JwtDecoderFactory bean。

例如,您可能有如下内容:

代码语言:javascript
复制
@Bean
public JwtDecoderFactory<ClientRegistration> customJwtDecoderFactory() {
    return new CustomJwtDecoderFactory();
}

static class CustomJwtDecoderFactory implements JwtDecoderFactory<ClientRegistration> {
    public JwtDecoder createDecoder(ClientRegistration reg) {
        //...

        return new CustomJwtDecoder();
    }
}

希望这至少能部分回答你的问题。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59744791

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档