尝试解码 Jwt 时发生错误:签名 JWT 被拒绝:需要另一种算法,或找不到匹配的密钥

Adn*_*ala 13 spring-security openam spring-oauth2 forgerock

我正在尝试使用与 Spring Security 集成的 ForgeRock OpenAM 设置 OAuth2-OpenID Connect,但收到以下错误

2019-06-17 15:01:42.576 DEBUG 62255 --- [nio-8090-exec-2] .o.s.r.w.BearerTokenAuthenticationFilter : 
Authentication request for failed: org.springframework.security.oauth2.core.OAuth2AuthenticationException: 
An error occurred while attempting to decode the Jwt: 
Signed JWT rejected: Another algorithm expected, or no matching key(s) found
Run Code Online (Sandbox Code Playgroud)

Jwk .wellknown uri 返回以下支持的算法:

"id_token_signing_alg_values_supported": [
    "PS384",
    "ES384",
    "RS384",
    "HS256",
    "HS512",
    "ES256",
    "RS256",
    "HS384",
    "ES512",
    "PS256",
    "PS512",
    "RS512"
  ]
Run Code Online (Sandbox Code Playgroud)

解码后的 JWT 显示以下标头:

{
  "typ": "JWT",
  "zip": "NONE",
  "alg": "HS256"
}
Run Code Online (Sandbox Code Playgroud)

有没有一种方法可以根据来自标头的值设置特定的 JwtDecoder 或强制 AM 使用一种特定算法?

Adn*_*ala 8

问题在于访问管理中令牌加密的配置。它是空白的,但由于某种原因,JWT 标头显示 HS256,这导致 spring 查找 HS256 私钥并失败。当我将设置更改为使用 RS256 后,一切都开始工作。

  • +1 对于KeyCloak 10.0.1,必须在领域/令牌下将“默认签名算法”设置为RS256(或您喜欢的任何内容) (3认同)

I A*_*OOT 6

就我而言,默认情况下NimbusJwtDecoder采用RS256JwsAlgo。因此,我配置JWTDecoder并提供了RS512在 JWT 标头中找到的算法。

{“alg”:“RS512”,“typ”:“JWT”}

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Value("${spring.security.oauth2.resourceserver.jwt.jwk-set-uri}")
    private String jwkSetUri;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().anyRequest().authenticated().and().oauth2ResourceServer().jwt().decoder(jwtDecoder());
    }

    @Bean
    public JwtDecoder jwtDecoder() {
        return NimbusJwtDecoder.withJwkSetUri(jwkSetUri).jwsAlgorithm(SignatureAlgorithm.RS512).build();
    }
}
Run Code Online (Sandbox Code Playgroud)