2

我正在尝试使用集成了 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 使用一种特定的算法?

4

3 回答 3

5

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

于 2019-06-23T01:39:23.967 回答
1

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

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

于 2019-06-18T15:28:04.293 回答
1

就我而言,默认情况下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();
    }
}
于 2022-02-03T09:21:07.760 回答