ClassCastException NimbusJwtDecoder 无法转换为 NimbusJwtDecoderJwkSupport

Posted

技术标签:

【中文标题】ClassCastException NimbusJwtDecoder 无法转换为 NimbusJwtDecoderJwkSupport【英文标题】:ClassCastException NimbusJwtDecoder cannot be cast to NimbusJwtDecoderJwkSupport 【发布时间】:2020-10-04 20:47:53 【问题描述】:

我在从2.1.5 升级到Spring boot 2.3.0 时遇到spring-security 的错误

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springSecurityFilterChain' defined in class path resource [org/springframework/security/config/annotation/web/configuration/WebSecurityConfiguration.class]: Bean instantiation via factory method failed; 
nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.servlet.Filter]: Factory method 'springSecurityFilterChain' threw exception; nested exception is java.lang.ClassCastException: org.springframework.security.oauth2.jwt.NimbusJwtDecoder cannot be cast to org.springframework.security.oauth2.jwt.NimbusJwtDecoderJwkSupport

我的安全配置是

public class OpenIdSecurityConfiguration extends WebSecurityConfigurerAdapter 

    @Autowired
    private OAuth2ResourceServerProperties resourceServerProperties;

    @Autowired
    private MyJwtValidator myJwtValidator;

    @Override
    protected void configure(HttpSecurity http) throws Exception 
        super.configure(http);

      http.authorizeRequests().anyRequest().authenticated().and()
                .oauth2ResourceServer().jwt().decoder(jwtDecoder())
                .jwtAuthenticationConverter(new MyAuthenticationConverter());
    

    private JwtDecoder jwtDecoder() 

        String issuerUri = this.resourceServerProperties.getJwt().getIssuerUri();

        NimbusJwtDecoderJwkSupport jwtDecoder = (NimbusJwtDecoderJwkSupport) JwtDecoders
                .fromOidcIssuerLocation(issuerUri);

        OAuth2TokenValidator<Jwt> issuer = JwtValidators.createDefaultWithIssuer(issuerUri);
        OAuth2TokenValidator<Jwt> myIssuer = new DelegatingOAuth2TokenValidator<>(issuer, myJwtValidator);

        jwtDecoder.setJwtValidator(myIssuer);

        return jwtDecoder;
    

我相信在这两者之间发生了一些变化,但我无法得到它。 任何帮助或想法都会得到重视 谢谢!!

【问题讨论】:

【参考方案1】:

在 NimbusJwtDecoderJwkSupport 的状态下使用 NimbusJwtDecoder

https://docs.spring.io/spring-security/site/docs/current/api/org/springframework/security/oauth2/jwt/NimbusJwtDecoder.html

【讨论】:

以上是关于ClassCastException NimbusJwtDecoder 无法转换为 NimbusJwtDecoderJwkSupport的主要内容,如果未能解决你的问题,请参考以下文章

Storm Nimbus默认的任务调度策略

storm:防火墙引起启动nimbus失败

Nimbus 和备用行颜色

Nimbus 游戏手柄方向键映射?

在 nimbus-jose-jwt 中,lifespan 和 refreshTime 有啥区别?

更改 Nimbus LaF 处理 JTree 节点突出显示的方式