【问题标题】:An error occurred while attempting to decode the Jwt: Signed JWT rejected: Another algorithm expected, or no matching key(s) found尝试解码 Jwt 时出错:签名的 JWT 被拒绝:需要另一个算法,或者找不到匹配的密钥
【发布时间】:2019-10-31 11:11:29
【问题描述】:

我正在尝试使用集成了 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

Jwk .well-known uri 返回以下支持的算法:

"id_token_signing_alg_values_supported": [
    "PS384",
    "ES384",
    "RS384",
    "HS256",
    "HS512",
    "ES256",
    "RS256",
    "HS384",
    "ES512",
    "PS256",
    "PS512",
    "RS512"
  ]

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

{
  "typ": "JWT",
  "zip": "NONE",
  "alg": "HS256"
}

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

【问题讨论】:

    标签: spring-security openam spring-oauth2 forgerock


    【解决方案1】:

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

    【讨论】:

    • +1 对于 KeyCloak 10.0.1,必须在 Realm /Tokens 下将“默认签名算法”设置为 RS256(或任何您喜欢的)
    【解决方案2】:

    是的,您可以告诉 AM 对 OIDC id 令牌签名 (https://backstage.forgerock.com/docs/am/6.5/oidc1-guide/#configure-oauth2-oidc-client-signing) 使用特定的签名算法,但我怀疑客户端由于缺少密钥而无法验证签名。

    只是为了确保......您知道 OAuth2 和 OIDC 是不同的主题......

    【讨论】:

    • 感谢 Bernhard,但钥匙在。 OIDC 是 OAuth2 的扩展,所以我不会说它们是完全不同的主题。
    【解决方案3】:

    在我的情况下,默认情况下 NimbusJwtDecoderRS256 作为 JwsAlgo。所以我配置了JWTDecoder 并提供了我在JWT 标头中找到的RS512 算法。

    { "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();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-11-18
      • 2022-01-15
      • 1970-01-01
      • 2020-07-08
      • 2022-01-26
      • 2016-11-17
      • 2020-02-20
      • 2018-01-09
      • 2017-07-12
      相关资源
      最近更新 更多