2.3. 为声明签名
您可以立即或在配置 JSON Web 签名(JWS)标头后为声明签名 :
默认行为:
-
默认情况下
,alg (算法)标头设置为RS256。 -
如果使用包含
kid属性的单个 JSON Web Key (JWK),则不必设置签名密钥标识符(kid标头)。
支持的密钥和算法:
- 要签署声明,您可以使用 RSA 私钥、Eliptic Curve (EC)私钥和对称 secret 密钥。
-
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();
您可以将 签名步骤与 加密 步骤相结合,以创建 内部签名和加密 的令牌。如需更多信息,请参阅 签名声明并加密嵌套的 JWT 令牌 部分。