SSK
SSK

Reputation: 3766

ClassCastException NimbusJwtDecoder cannot be cast to NimbusJwtDecoderJwkSupport

I am getting below error for spring-security while upgrading to Spring boot 2.3.0 from 2.1.5

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

My Security configuration is

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;
    }
}

I believe something got changed in between but I am unable to get it.
Any help or thought will be appraciated
Thanks !!

Upvotes: 2

Views: 1655

Answers (1)

DBS
DBS

Reputation: 44

use NimbusJwtDecoder instate of NimbusJwtDecoderJwkSupport

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

Upvotes: 1

Related Questions