Class JwtClaims

java.lang.Object
cloud.opencode.base.crypto.jwt.JwtClaims

public final class JwtClaims extends Object
JWT Claims - Container for JWT claims (payload) JWT 声明 - JWT 声明(载荷)容器

Provides a fluent builder API for constructing JWT claims with both standard registered claims and custom claims.

提供流式构建器 API 用于构造 JWT 声明,支持标准注册声明和自定义声明。

Standard Claims (RFC 7519) | 标准声明:

  • iss (issuer) - 签发者
  • sub (subject) - 主题
  • aud (audience) - 受众
  • exp (expiration time) - 过期时间
  • nbf (not before) - 生效时间
  • iat (issued at) - 签发时间
  • jti (JWT ID) - JWT 唯一标识

Usage Examples | 使用示例:

JwtClaims claims = JwtClaims.builder()
    .issuer("auth-service")
    .subject("user123")
    .audience("api-service")
    .expiresIn(Duration.ofHours(1))
    .claim("role", "admin")
    .claim("permissions", List.of("read", "write"))
    .build();

Features | 主要功能:

  • See class description for details - 详见类描述

Security | 安全性:

  • Thread-safe: Yes - 线程安全: 是
  • Null-safe: Yes - 空值安全: 是
Since:
JDK 25, opencode-base-crypto V1.2.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Field Details

  • Method Details

    • builder

      public static JwtClaims.Builder builder()
      Creates a new claims builder. 创建新的声明构建器。
      Returns:
      a new builder
    • of

      public static JwtClaims of(Map<String,Object> claims)
      Creates claims from a map. 从映射创建声明。
      Parameters:
      claims - the claims map
      Returns:
      JwtClaims instance
    • empty

      public static JwtClaims empty()
      Creates empty claims. 创建空声明。
      Returns:
      empty JwtClaims instance
    • issuer

      public String issuer()
      Returns the issuer claim. 返回签发者声明。
      Returns:
      the issuer, or null if not present
    • subject

      public String subject()
      Returns the subject claim. 返回主题声明。
      Returns:
      the subject, or null if not present
    • audience

      public List<String> audience()
      Returns the audience claim. 返回受众声明。
      Returns:
      the audience, or null if not present
    • expiration

      public Instant expiration()
      Returns the expiration time. 返回过期时间。
      Returns:
      the expiration instant, or null if not present
    • notBefore

      public Instant notBefore()
      Returns the not-before time. 返回生效时间。
      Returns:
      the not-before instant, or null if not present
    • issuedAt

      public Instant issuedAt()
      Returns the issued-at time. 返回签发时间。
      Returns:
      the issued-at instant, or null if not present
    • jwtId

      public String jwtId()
      Returns the JWT ID. 返回 JWT ID。
      Returns:
      the JWT ID, or null if not present
    • get

      public Object get(String name)
      Returns a claim value. 返回声明值。
      Parameters:
      name - the claim name
      Returns:
      the claim value, or null if not present
    • getString

      public String getString(String name)
      Returns a claim value as String. 返回字符串类型的声明值。
      Parameters:
      name - the claim name
      Returns:
      the claim value as string, or null if not present
    • getInt

      public Integer getInt(String name)
      Returns a claim value as Integer. 返回整数类型的声明值。
      Parameters:
      name - the claim name
      Returns:
      the claim value as integer, or null if not present
    • getLong

      public Long getLong(String name)
      Returns a claim value as Long. 返回长整数类型的声明值。
      Parameters:
      name - the claim name
      Returns:
      the claim value as long, or null if not present
    • getBoolean

      public Boolean getBoolean(String name)
      Returns a claim value as Boolean. 返回布尔类型的声明值。
      Parameters:
      name - the claim name
      Returns:
      the claim value as boolean, or null if not present
    • getInstant

      public Instant getInstant(String name)
      Returns a claim value as Instant. 返回 Instant 类型的声明值。
      Parameters:
      name - the claim name
      Returns:
      the claim value as instant, or null if not present
    • getList

      public <T> List<T> getList(String name)
      Returns a claim value as List. 返回列表类型的声明值。
      Type Parameters:
      T - the element type
      Parameters:
      name - the claim name
      Returns:
      the claim value as list, or null if not present
    • contains

      public boolean contains(String name)
      Checks if a claim is present. 检查声明是否存在。
      Parameters:
      name - the claim name
      Returns:
      true if the claim exists
    • names

      public Set<String> names()
      Returns all claim names. 返回所有声明名称。
      Returns:
      the set of claim names
    • asMap

      public Map<String,Object> asMap()
      Returns the claims as a map. 返回声明映射。
      Returns:
      unmodifiable claims map
    • isExpired

      public boolean isExpired()
      Checks if the token has expired. 检查令牌是否已过期。
      Returns:
      true if expired
    • isNotYetValid

      public boolean isNotYetValid()
      Checks if the token is not yet valid. 检查令牌是否尚未生效。
      Returns:
      true if not yet valid
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object