Record Class JwtClaims

java.lang.Object
java.lang.Record
cloud.opencode.base.oauth2.oidc.JwtClaims

public record JwtClaims(String iss, String sub, List<String> aud, Instant exp, Instant nbf, Instant iat, String jti, String nonce, String azp, Map<String,Object> claims) extends Record
JWT Claims JWT 声明

Represents decoded JWT claims from an ID token or access token.

表示从 ID 令牌或访问令牌解码的 JWT 声明。

Note | 注意:

This parser does NOT verify the JWT signature. For security-critical applications, use a proper JWT library with signature verification.

此解析器不验证 JWT 签名。对于安全关键的应用程序,请使用具有签名验证的正式 JWT 库。

Usage Examples | 使用示例:

// Parse JWT without verification
JwtClaims claims = JwtClaims.parse(idToken);

String subject = claims.sub();
String issuer = claims.iss();
Instant expiry = claims.exp();

// Check if expired
if (claims.isExpired()) {
    // Token is expired
}

Features | 主要功能:

  • JWT claims representation for OIDC tokens - OIDC令牌的JWT声明表示
  • Standard claim accessors (sub, iss, exp, iat) - 标准声明访问器(sub、iss、exp、iat)

Security | 安全性:

  • Thread-safe: Yes (immutable record) - 线程安全: 是(不可变记录)
  • Null-safe: Yes (validates inputs) - 空值安全: 是(验证输入)
Since:
JDK 25, opencode-base-oauth2 V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    JwtClaims(String iss, String sub, List<String> aud, Instant exp, Instant nbf, Instant iat, String jti, String nonce, String azp, Map<String,Object> claims)
    Compact constructor 紧凑构造器
  • Method Summary

    Modifier and Type
    Method
    Description
    aud()
    Returns the value of the aud record component.
    Get the first audience 获取第一个受众
    azp()
    Returns the value of the azp record component.
    Returns the value of the claims record component.
    final boolean
    Indicates whether some other object is "equal to" this one.
    exp()
    Returns the value of the exp record component.
    Get a custom claim value 获取自定义声明值
    Get a custom claim value as string 获取自定义声明值作为字符串
    boolean
    hasAudience(String audience)
    Check if the token has the specified audience 检查令牌是否具有指定的受众
    final int
    Returns a hash code value for this object.
    iat()
    Returns the value of the iat record component.
    boolean
    Check if the token is expired 检查令牌是否已过期
    boolean
    Check if the token is not yet valid 检查令牌是否尚未生效
    iss()
    Returns the value of the iss record component.
    boolean
    Check if the token is currently valid (not expired and not before nbf) 检查令牌当前是否有效(未过期且不在 nbf 之前)
    jti()
    Returns the value of the jti record component.
    nbf()
    Returns the value of the nbf record component.
    Returns the value of the nonce record component.
    static JwtClaims
    parse(String token)
    Parse a JWT token (without signature verification) 解析 JWT 令牌(不验证签名)
    sub()
    Returns the value of the sub record component.
    final String
    Returns a string representation of this record class.

    Methods inherited from class Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

  • Method Details

    • isExpired

      public boolean isExpired()
      Check if the token is expired 检查令牌是否已过期
      Returns:
      true if expired | 已过期返回 true
    • isNotYetValid

      public boolean isNotYetValid()
      Check if the token is not yet valid 检查令牌是否尚未生效
      Returns:
      true if not yet valid | 尚未生效返回 true
    • isValid

      public boolean isValid()
      Check if the token is currently valid (not expired and not before nbf) 检查令牌当前是否有效(未过期且不在 nbf 之前)
      Returns:
      true if valid | 有效返回 true
    • getClaim

      public Optional<Object> getClaim(String name)
      Get a custom claim value 获取自定义声明值
      Parameters:
      name - the claim name | 声明名称
      Returns:
      the claim value | 声明值
    • getClaimAsString

      public Optional<String> getClaimAsString(String name)
      Get a custom claim value as string 获取自定义声明值作为字符串
      Parameters:
      name - the claim name | 声明名称
      Returns:
      the claim value as string | 声明值作为字符串
    • audience

      public String audience()
      Get the first audience 获取第一个受众
      Returns:
      the first audience or null | 第一个受众或 null
    • hasAudience

      public boolean hasAudience(String audience)
      Check if the token has the specified audience 检查令牌是否具有指定的受众
      Parameters:
      audience - the audience to check | 要检查的受众
      Returns:
      true if has audience | 有受众返回 true
    • parse

      public static JwtClaims parse(String token)
      Parse a JWT token (without signature verification) 解析 JWT 令牌(不验证签名)
      Parameters:
      token - the JWT token | JWT 令牌
      Returns:
      the claims | 声明
      Throws:
      OAuth2Exception - if parsing fails | 如果解析失败
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with Objects::equals(Object,Object).
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • iss

      public String iss()
      Returns the value of the iss record component.
      Returns:
      the value of the iss record component
    • sub

      public String sub()
      Returns the value of the sub record component.
      Returns:
      the value of the sub record component
    • aud

      public List<String> aud()
      Returns the value of the aud record component.
      Returns:
      the value of the aud record component
    • exp

      public Instant exp()
      Returns the value of the exp record component.
      Returns:
      the value of the exp record component
    • nbf

      public Instant nbf()
      Returns the value of the nbf record component.
      Returns:
      the value of the nbf record component
    • iat

      public Instant iat()
      Returns the value of the iat record component.
      Returns:
      the value of the iat record component
    • jti

      public String jti()
      Returns the value of the jti record component.
      Returns:
      the value of the jti record component
    • nonce

      public String nonce()
      Returns the value of the nonce record component.
      Returns:
      the value of the nonce record component
    • azp

      public String azp()
      Returns the value of the azp record component.
      Returns:
      the value of the azp record component
    • claims

      public Map<String,Object> claims()
      Returns the value of the claims record component.
      Returns:
      the value of the claims record component