2.3. クレームの署名
クレームは、すぐに署名することも、JSON Web Signature (JWS) ヘッダーを設定した後に署名することもできます。
デフォルトの動作:
-
alg(アルゴリズム) ヘッダーはデフォルトでRS256に設定されます。 -
kidプロパティーを含む単一の JSON Web Key (JWK) が使用される場合、署名鍵の識別子 (kidヘッダー) を設定する必要はありません。
サポートされているキーとアルゴリズム:
- クレームに署名するには、RSA 秘密鍵、楕円曲線 (EC) 秘密鍵、対称シークレットキーを使用できます。
-
RS256はデフォルトの RSA 秘密鍵署名アルゴリズムです。 -
ES256はデフォルトの EC 秘密鍵署名アルゴリズムです。 -
HS256はデフォルトの対称鍵署名アルゴリズムです。
署名アルゴリズムをカスタマイズするには、JwtSignatureBuilder API を使用します。以下に例を示します。
import io.smallrye.jwt.SignatureAlgorithm;
import io.smallrye.jwt.build.Jwt;
// Sign the claims using an RSA private key loaded from the location set with a 'smallrye.jwt.sign.key.location' property. The algorithm is PS256.
String jwt = Jwt.upn("Alice").jws().algorithm(SignatureAlgorithm.PS256).sign();
import io.smallrye.jwt.SignatureAlgorithm;
import io.smallrye.jwt.build.Jwt;
// Sign the claims using an RSA private key loaded from the location set with a 'smallrye.jwt.sign.key.location' property. The algorithm is PS256.
String jwt = Jwt.upn("Alice").jws().algorithm(SignatureAlgorithm.PS256).sign();
または、次のプロパティーを使用して署名アルゴリズムをグローバルに設定することもできます。
smallrye.jwt.new-token.signature-algorithm=PS256
smallrye.jwt.new-token.signature-algorithm=PS256
このアプローチにより、よりシンプルな API シーケンスが実現します。
import io.smallrye.jwt.build.Jwt;
// Sign the claims using an RSA private key loaded from the location set with a 'smallrye.jwt.sign.key.location' property. The algorithm is PS256.
String jwt = Jwt.upn("Alice").sign();
import io.smallrye.jwt.build.Jwt;
// Sign the claims using an RSA private key loaded from the location set with a 'smallrye.jwt.sign.key.location' property. The algorithm is PS256.
String jwt = Jwt.upn("Alice").sign();
sign ステップと 暗号化 ステップを組み合わせて、inner-signed and encrypted トークンを作成できます。詳細は、クレームに署名し、ネストされた JWT トークンを暗号化する セクションを参照してください。