本文へスキップ
ecdsa.com
Nimbus JOSE + JWT (Java)Java

エラーメッセージ

Signed JWT rejected: Another algorithm expected, or no matching key(s) found

別の表記

  • com.nimbusds.jose.proc.BadJOSEException: Signed JWT rejected: Another algorithm expected, or no matching key(s) found
  • org.springframework.security.oauth2.jwt.BadJwtException: An error occurred while attempting to decode the Jwt: Signed JWT rejected: Another algorithm expected, or no matching key(s) found

Nimbus JOSE + JWT (Java) — BadJOSEException from DefaultJWTProcessor; also surfaced by Spring Security's NimbusJwtDecoder

このエラーの意味

Nimbus の鍵セレクターが、このトークンに対する候補の鍵を 1 つも返しませんでした。トークンの `alg` がセレクターの受け付けるアルゴリズムに含まれていないか、JWK セット内にトークンの `kid` と鍵の種類に一致する鍵がないかのどちらかです。Nimbus は、どちらだったのかをあえて示しません。Spring Security 経由でこのエラーが出る場合は、`NimbusJwtDecoder` のデフォルトの RS256 が、ES256 で署名する発行者のトークンを受け取ったことが原因であることが非常に多くあります。

発生する理由

修正方法

  1. 1.

    発行者が使うアルゴリズムをデコーダーに指定する

    ビルダーで `jwsAlgorithm(...)`(または `jwsAlgorithms(...)`)を指定すると、デフォルトの RS256 が置き換えられます。リストには、発行者が実際に使うアルゴリズムだけを含めてください。

    java
    @Bean
    JwtDecoder jwtDecoder() {
      return NimbusJwtDecoder.withIssuerLocation("https://issuer.example.com/realms/main")
          .jwsAlgorithm(SignatureAlgorithm.ES256) // the issuer's algorithm; RS256 is only the default
          .build();
    }
  2. 2.

    または application.yml で設定する

    Spring Boot では、同じ設定を `jws-algorithms` として指定できます。

    yaml
    spring:
      security:
        oauth2:
          resourceserver:
            jwt:
              issuer-uri: https://issuer.example.com/realms/main
              jws-algorithms: ES256

関連するエラー

署名エラーデータベースをすべて見る