Class JwtUtil.Builder
java.lang.Object
cloud.opencode.base.crypto.jwt.JwtUtil.Builder
- Enclosing class:
JwtUtil
JWT Builder - Fluent builder for creating JWTs
JWT 构建器 - 用于创建 JWT 的流式构建器
- Since:
- JDK 25, opencode-base-crypto V1.2.0
- Author:
- Leon Soo www.LeonSoo.com
-
Method Summary
Modifier and TypeMethodDescriptionalgorithm(JwtAlgorithm algorithm) Sets the signing algorithm.Sets the audience claim.Sets the audience claim with multiple values.Sets a custom claim.Sets multiple claims.Sets multiple claims from a map.expiration(Instant expiration) Sets the expiration time.Sets the expiration time relative to now.Generates a random JWT ID.Adds a header claim.Sets the issued-at time.Sets the issued-at time to now.Sets the issuer claim.Sets the not-before time.privateKey(PrivateKey privateKey) Sets the private key for asymmetric signing.secret(byte[] secret) Sets the HMAC secret bytes.Sets the HMAC secret.sign()Signs and returns the JWT string.Sets the subject claim.
-
Method Details
-
algorithm
Sets the signing algorithm. 设置签名算法。- Parameters:
algorithm- the algorithm- Returns:
- this builder
-
secret
Sets the HMAC secret. 设置 HMAC 密钥。- Parameters:
secret- the secret string- Returns:
- this builder
-
secret
Sets the HMAC secret bytes. 设置 HMAC 密钥字节。- Parameters:
secret- the secret bytes- Returns:
- this builder
-
privateKey
Sets the private key for asymmetric signing. 设置用于非对称签名的私钥。- Parameters:
privateKey- the private key- Returns:
- this builder
-
header
Adds a header claim. 添加头部声明。- Parameters:
name- the claim namevalue- the claim value- Returns:
- this builder
-
issuer
Sets the issuer claim. 设置签发者声明。- Parameters:
issuer- the issuer- Returns:
- this builder
-
subject
Sets the subject claim. 设置主题声明。- Parameters:
subject- the subject- Returns:
- this builder
-
audience
Sets the audience claim. 设置受众声明。- Parameters:
audience- the audience- Returns:
- this builder
-
audience
Sets the audience claim with multiple values. 设置多值受众声明。- Parameters:
audiences- the audiences- Returns:
- this builder
-
expiration
Sets the expiration time. 设置过期时间。- Parameters:
expiration- the expiration instant- Returns:
- this builder
-
expiresIn
Sets the expiration time relative to now. 设置相对于当前时间的过期时间。- Parameters:
duration- the duration from now- Returns:
- this builder
-
notBefore
Sets the not-before time. 设置生效时间。- Parameters:
notBefore- the not-before instant- Returns:
- this builder
-
issuedAtNow
Sets the issued-at time to now. 设置签发时间为当前时间。- Returns:
- this builder
-
issuedAt
Sets the issued-at time. 设置签发时间。- Parameters:
issuedAt- the issued-at instant- Returns:
- this builder
-
generateJwtId
Generates a random JWT ID. 生成随机 JWT ID。- Returns:
- this builder
-
claim
Sets a custom claim. 设置自定义声明。- Parameters:
name- the claim namevalue- the claim value- Returns:
- this builder
-
claims
Sets multiple claims. 设置多个声明。- Parameters:
claims- the claims- Returns:
- this builder
-
claims
Sets multiple claims from a map. 从映射设置多个声明。- Parameters:
claims- the claims map- Returns:
- this builder
-
sign
Signs and returns the JWT string. 签名并返回 JWT 字符串。- Returns:
- the JWT string
- Throws:
OpenCryptoException- if signing fails
-