Spring Security 尝试解码JWT时出错:已拒绝签名的JWT:需要另一个算法,或未找到匹配的键

gpnt7bae  于 5个月前  发布在  Spring
关注(0)|答案(4)|浏览(175)

我正在尝试安装OAuth2-OpenID连接与ForgeRock OpenAM集成与Spring的安全性,并得到以下错误

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使用一种特定的算法?

w6mmgewl

w6mmgewl1#

这个问题是与令牌加密的访问管理中的配置.它是空白的,但由于某种原因,JWT头显示HS256,这导致Spring寻找HS256私钥和失败.在我改变设置使用RS256后,一切都开始工作.

qoefvg9y

qoefvg9y2#

在我的例子中,默认情况下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();
    }
}

字符串

owfi6suc

owfi6suc3#

是的,您可以告诉AM使用特定的签名算法进行OIDC ID令牌签名(https://backstage.forgerock.com/docs/am/6.5/oidc1-guide/#configure-oauth2-oidc-client-signing),但我怀疑客户端由于缺少密钥而无法验证签名。
只是为了确保.你知道OAuth2和OIDC是不同的主题.

wz3gfoph

wz3gfoph4#

另一种获得此类错误的方法是验证路径中存在JWT URI不一致性沿着。
例如,JWT发布者URI可能已经在服务器侧上用特定区域或用户池硬编码,诸如“https://cognito-idp.us-east-1.amazonaws.com/us-east-1_abcdefghi“。
使用硬编码值时要小心。

相关问题