Class JwtUtil.Builder

java.lang.Object
cloud.opencode.base.crypto.jwt.JwtUtil.Builder
Enclosing class:
JwtUtil

public static final class JwtUtil.Builder extends Object
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 Details

    • algorithm

      public JwtUtil.Builder algorithm(JwtAlgorithm algorithm)
      Sets the signing algorithm. 设置签名算法。
      Parameters:
      algorithm - the algorithm
      Returns:
      this builder
    • secret

      public JwtUtil.Builder secret(String secret)
      Sets the HMAC secret. 设置 HMAC 密钥。
      Parameters:
      secret - the secret string
      Returns:
      this builder
    • secret

      public JwtUtil.Builder secret(byte[] secret)
      Sets the HMAC secret bytes. 设置 HMAC 密钥字节。
      Parameters:
      secret - the secret bytes
      Returns:
      this builder
    • privateKey

      public JwtUtil.Builder privateKey(PrivateKey privateKey)
      Sets the private key for asymmetric signing. 设置用于非对称签名的私钥。
      Parameters:
      privateKey - the private key
      Returns:
      this builder
    • header

      public JwtUtil.Builder header(String name, Object value)
      Adds a header claim. 添加头部声明。
      Parameters:
      name - the claim name
      value - the claim value
      Returns:
      this builder
    • issuer

      public JwtUtil.Builder issuer(String issuer)
      Sets the issuer claim. 设置签发者声明。
      Parameters:
      issuer - the issuer
      Returns:
      this builder
    • subject

      public JwtUtil.Builder subject(String subject)
      Sets the subject claim. 设置主题声明。
      Parameters:
      subject - the subject
      Returns:
      this builder
    • audience

      public JwtUtil.Builder audience(String audience)
      Sets the audience claim. 设置受众声明。
      Parameters:
      audience - the audience
      Returns:
      this builder
    • audience

      public JwtUtil.Builder audience(List<String> audiences)
      Sets the audience claim with multiple values. 设置多值受众声明。
      Parameters:
      audiences - the audiences
      Returns:
      this builder
    • expiration

      public JwtUtil.Builder expiration(Instant expiration)
      Sets the expiration time. 设置过期时间。
      Parameters:
      expiration - the expiration instant
      Returns:
      this builder
    • expiresIn

      public JwtUtil.Builder expiresIn(Duration duration)
      Sets the expiration time relative to now. 设置相对于当前时间的过期时间。
      Parameters:
      duration - the duration from now
      Returns:
      this builder
    • notBefore

      public JwtUtil.Builder notBefore(Instant notBefore)
      Sets the not-before time. 设置生效时间。
      Parameters:
      notBefore - the not-before instant
      Returns:
      this builder
    • issuedAtNow

      public JwtUtil.Builder issuedAtNow()
      Sets the issued-at time to now. 设置签发时间为当前时间。
      Returns:
      this builder
    • issuedAt

      public JwtUtil.Builder issuedAt(Instant issuedAt)
      Sets the issued-at time. 设置签发时间。
      Parameters:
      issuedAt - the issued-at instant
      Returns:
      this builder
    • generateJwtId

      public JwtUtil.Builder generateJwtId()
      Generates a random JWT ID. 生成随机 JWT ID。
      Returns:
      this builder
    • claim

      public JwtUtil.Builder claim(String name, Object value)
      Sets a custom claim. 设置自定义声明。
      Parameters:
      name - the claim name
      value - the claim value
      Returns:
      this builder
    • claims

      public JwtUtil.Builder claims(JwtClaims claims)
      Sets multiple claims. 设置多个声明。
      Parameters:
      claims - the claims
      Returns:
      this builder
    • claims

      public JwtUtil.Builder claims(Map<String,Object> claims)
      Sets multiple claims from a map. 从映射设置多个声明。
      Parameters:
      claims - the claims map
      Returns:
      this builder
    • sign

      public String sign()
      Signs and returns the JWT string. 签名并返回 JWT 字符串。
      Returns:
      the JWT string
      Throws:
      OpenCryptoException - if signing fails