spring jwt 解码器 openid 令牌

Unr*_*an1 2 spring spring-security oauth-2.0 spring-security-oauth2

外部 OAuth2 提供程序没有公共 JwkUri,因此我也尝试使用以下代码片段覆盖默认行为:

@EnableWebSecurity
public class DirectlyConfiguredJwkSetUri extends WebSecurityConfigurerAdapter {
 @Override
protected void configure(HttpSecurity http) throws Exception {
    http
            .authorizeRequests()
            .antMatchers("**/oauth2/code/esia**", "**/code/esia**", "**esia**").permitAll()
            .antMatchers("/user").fullyAuthenticated()
            .anyRequest().authenticated()
            .and()
            .csrf().disable()
            .cors().disable()
            .oauth2Client()
            .clientRegistrationRepository(this.clientRegistrationRepository)
            .authorizationCodeGrant()
            .authorizationRequestResolver(new CustomAuthorizationRequestResolver(
                    this.clientRegistrationRepository, esiaConfig, signatureUtil, timeUtil))
            .accessTokenResponseClient(customAccessTokenResponseClient())
            .and().and().oauth2Login().tokenEndpoint().accessTokenResponseClient(customAccessTokenResponseClient())
            .and().and().oauth2ResourceServer().jwt();
}
@Bean
JwtDecoder jwtDecoder() {
 return  new CustomJwtDecoder();
}
}

class CustomJwtDecoder implements JwtDecoder {
@Override
public Jwt decode(String token) throws JwtException {
    System.out.println(token);
    return null;
}
}
Run Code Online (Sandbox Code Playgroud)

然而 Spring Security 不知何故仍然使用默认实现,我收到以下错误......

[missing_signature_verifier] Failed to find a Signature Verifier for Client Registration: 'esia'. Check to ensure you have configured the JwkSet URI.
Run Code Online (Sandbox Code Playgroud)

另外,我尝试设置自定义 AuthenticationProvider 但 spring 忽略它。

我想问题是 spring 的 OAuth2LoginConfigurer 方法 init(B http) 调用 new OidcAuthorizationCodeAuthenticationProvider(accessTokenResponseClient, oidcUserService)

小智 5

即使使用 5.2.x 版本,我也面临同样的问题。就我而言,真正的问题不在 JwtDecoder 中。我通过设置 jwk-set-uri 属性解决了这个问题(您可以通过您正在使用的提供商更改提供商名称,例如 okta、google 等):

security.oauth2.client.provider.azure.jwk-set-uri: https://login.microsoftonline.com/{tenant}/discovery/keys

Run Code Online (Sandbox Code Playgroud)