Package com.nimbusds.jwt
Class JWTClaimsSet
java.lang.Object
com.nimbusds.jwt.JWTClaimsSet
- All Implemented Interfaces:
Serializable
JSON Web Token (JWT) claims set. This class is immutable.
Supports all registered claims
of the JWT
specification:
- iss - Issuer
- sub - Subject
- aud - Audience
- exp - Expiration Time
- nbf - Not Before
- iat - Issued At
- jti - JWT ID
The set may also contain custom claims.
Claims with null
values will not be serialised with
toPayload()
/ toJSONObject()
/ toString()
unless
JWTClaimsSet.Builder.serializeNullClaims
is enabled.
Example JWT claims set:
{ "sub" : "joe", "exp" : 1300819380, "https://example.com/is_root" : true }
Example usage:
JWTClaimsSet claimsSet = new JWTClaimsSet.Builder() .subject("joe") .expirationTime(new Date(1300819380 * 1000l) .claim("http://example.com/is_root", true) .build();
- Version:
- 2024-12-20
- Author:
- Vladimir Dzhuvinov, Justin Richer, Joey Zhao
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
Builder for constructing JSON Web Token (JWT) claims sets. -
Method Summary
Modifier and TypeMethodDescriptionboolean
Gets the audience (aud
) claim.getBooleanClaim
(String name) Gets the specified claim (registered or custom) asBoolean
.Gets the specified claim (registered or custom).getClaimAsString
(String name) Gets the claims (registered and custom).getDateClaim
(String name) Gets the specified claim (registered or custom) asDate
.getDoubleClaim
(String name) Gets the specified claim (registered or custom) asDouble
.Gets the expiration time (exp
) claim.getFloatClaim
(String name) Gets the specified claim (registered or custom) asFloat
.getIntegerClaim
(String name) Gets the specified claim (registered or custom) asInteger
.Gets the issuer (iss
) claim.Gets the issued-at (iat
) claim.getJSONObjectClaim
(String name) Gets the specified claim (registered or custom) as a JSON object.getJWTID()
Gets the JWT ID (jti
) claim.getListClaim
(String name) Gets the specified claims (registered or custom) as aList
list of objects.getLongClaim
(String name) Gets the specified claim (registered or custom) asLong
.Gets the not-before (nbf
) claim.Gets the registered JWT claim names.String[]
getStringArrayClaim
(String name) Gets the specified claims (registered or custom) as aString
array.getStringClaim
(String name) Gets the specified claim (registered or custom) asString
.getStringListClaim
(String name) Gets the specified claims (registered or custom) as aList
list of strings.Gets the subject (sub
) claim.getURIClaim
(String name) Gets the specified claim (registered or custom) as aURI
.int
hashCode()
static JWTClaimsSet
Parses a JSON Web Token (JWT) claims set from the specified JSON object string representation.static JWTClaimsSet
Parses a JSON Web Token (JWT) claims set from the specified JSON object representation.Returns the JSON object representation of this claims set.toJSONObject
(boolean serializeNullClaims) Returns the JSON object representation of this claims set.Returns a JOSE object payload representation of this claims set.toPayload
(boolean serializeNullClaims) Returns a JOSE object payload representation of this claims set.toString()
Returns a JSON object string representation of this claims set.toString
(boolean serializeNullClaims) Returns a JSON object string representation of this claims set.<T> T
toType
(JWTClaimsSetTransformer<T> transformer) Returns a transformation of this JWT claims set.
-
Method Details
-
getRegisteredNames
Gets the registered JWT claim names.- Returns:
- The registered claim names, as an unmodifiable set.
-
getIssuer
Gets the issuer (iss
) claim.- Returns:
- The issuer claim,
null
if not specified.
-
getSubject
Gets the subject (sub
) claim.- Returns:
- The subject claim,
null
if not specified.
-
getAudience
Gets the audience (aud
) claim.- Returns:
- The audience claim, empty list if not specified.
-
getExpirationTime
Gets the expiration time (exp
) claim.- Returns:
- The expiration time,
null
if not specified.
-
getNotBeforeTime
Gets the not-before (nbf
) claim.- Returns:
- The not-before claim,
null
if not specified.
-
getIssueTime
Gets the issued-at (iat
) claim.- Returns:
- The issued-at claim,
null
if not specified.
-
getJWTID
Gets the JWT ID (jti
) claim.- Returns:
- The JWT ID claim,
null
if not specified.
-
getClaim
Gets the specified claim (registered or custom).- Parameters:
name
- The name of the claim. Must not benull
.- Returns:
- The value of the claim,
null
if not specified.
-
getStringClaim
Gets the specified claim (registered or custom) asString
.- Parameters:
name
- The name of the claim. Must not benull
.- Returns:
- The value of the claim,
null
if not specified. - Throws:
ParseException
- If the claim value is not of the required type.
-
getClaimAsString
Gets the specified claim (registered or custom) asString
, primitive or Wrapper types will be converted toString
.- Parameters:
name
- The name of the claim. Must not benull
.- Returns:
- The value of the claim,
null
if not specified. - Throws:
ParseException
- If the claim value is not and cannot be automatically converted toString
.
-
getListClaim
Gets the specified claims (registered or custom) as aList
list of objects.- Parameters:
name
- The name of the claim. Must not benull
.- Returns:
- The value of the claim,
null
if not specified. - Throws:
ParseException
- If the claim value is not of the required type.
-
getStringArrayClaim
Gets the specified claims (registered or custom) as aString
array.- Parameters:
name
- The name of the claim. Must not benull
.- Returns:
- The value of the claim,
null
if not specified. - Throws:
ParseException
- If the claim value is not of the required type.
-
getStringListClaim
Gets the specified claims (registered or custom) as aList
list of strings.- Parameters:
name
- The name of the claim. Must not benull
.- Returns:
- The value of the claim,
null
if not specified. - Throws:
ParseException
- If the claim value is not of the required type.
-
getURIClaim
Gets the specified claim (registered or custom) as aURI
.- Parameters:
name
- The name of the claim. Must not benull
.- Returns:
- The value of the claim,
null
if not specified. - Throws:
ParseException
- If the claim couldn't be parsed to a URI.
-
getBooleanClaim
Gets the specified claim (registered or custom) asBoolean
.- Parameters:
name
- The name of the claim. Must not benull
.- Returns:
- The value of the claim,
null
if not specified. - Throws:
ParseException
- If the claim value is not of the required type.
-
getIntegerClaim
Gets the specified claim (registered or custom) asInteger
.- Parameters:
name
- The name of the claim. Must not benull
.- Returns:
- The value of the claim,
null
if not specified. - Throws:
ParseException
- If the claim value is not of the required type.
-
getLongClaim
Gets the specified claim (registered or custom) asLong
.- Parameters:
name
- The name of the claim. Must not benull
.- Returns:
- The value of the claim,
null
if not specified. - Throws:
ParseException
- If the claim value is not of the required type.
-
getDateClaim
Gets the specified claim (registered or custom) asDate
. The claim may be represented by a Date object or a number of a seconds since the Unix epoch.- Parameters:
name
- The name of the claim. Must not benull
.- Returns:
- The value of the claim,
null
if not specified. - Throws:
ParseException
- If the claim value is not of the required type.
-
getFloatClaim
Gets the specified claim (registered or custom) asFloat
.- Parameters:
name
- The name of the claim. Must not benull
.- Returns:
- The value of the claim,
null
if not specified. - Throws:
ParseException
- If the claim value is not of the required type.
-
getDoubleClaim
Gets the specified claim (registered or custom) asDouble
.- Parameters:
name
- The name of the claim. Must not benull
.- Returns:
- The value of the claim,
null
if not specified. - Throws:
ParseException
- If the claim value is not of the required type.
-
getJSONObjectClaim
Gets the specified claim (registered or custom) as a JSON object.- Parameters:
name
- The name of the claim. Must not benull
.- Returns:
- The value of the claim,
null
if not specified. - Throws:
ParseException
- If the claim value is not of the required type.
-
getClaims
Gets the claims (registered and custom).Note that the registered claims Expiration-Time (
exp
), Not-Before-Time (nbf
) and Issued-At (iat
) will be returned asjava.util.Date
instances.- Returns:
- The claims, as an unmodifiable map, empty map if none.
-
toPayload
Returns a JOSE object payload representation of this claims set. The claims are serialised according to their insertion order. Claims withnull
values are output according toJWTClaimsSet.Builder.serializeNullClaims(boolean)
.- Returns:
- The payload representation.
-
toPayload
Returns a JOSE object payload representation of this claims set. The claims are serialised according to their insertion order.- Parameters:
serializeNullClaims
-true
to serialise claims withnull
values,false
to omit them.- Returns:
- The payload representation.
-
toJSONObject
Returns the JSON object representation of this claims set. The claims are serialised according to their insertion order. Claims withnull
values are output according toJWTClaimsSet.Builder.serializeNullClaims(boolean)
.- Returns:
- The JSON object representation.
-
toJSONObject
Returns the JSON object representation of this claims set. The claims are serialised according to their insertion order.- Parameters:
serializeNullClaims
-true
to serialise claims withnull
values,false
to omit them.- Returns:
- The JSON object representation.
-
toString
Returns a JSON object string representation of this claims set. The claims are serialised according to their insertion order. Claims withnull
values are output according toJWTClaimsSet.Builder.serializeNullClaims(boolean)
. -
toString
Returns a JSON object string representation of this claims set. The claims are serialised according to their insertion order.- Parameters:
serializeNullClaims
-true
to serialise claims withnull
values,false
to omit them.- Returns:
- The JSON object string representation.
-
toType
Returns a transformation of this JWT claims set.- Type Parameters:
T
- Type of the result.- Parameters:
transformer
- The JWT claims set transformer. Must not benull
.- Returns:
- The transformed JWT claims set.
-
parse
Parses a JSON Web Token (JWT) claims set from the specified JSON object representation.- Parameters:
json
- The JSON object to parse. Must not benull
.- Returns:
- The JWT claims set.
- Throws:
ParseException
- If the specified JSON object doesn't represent a valid JWT claims set.
-
parse
Parses a JSON Web Token (JWT) claims set from the specified JSON object string representation.- Parameters:
s
- The JSON object string to parse. Must not benull
.- Returns:
- The JWT claims set.
- Throws:
ParseException
- If the specified JSON object string doesn't represent a valid JWT claims set.
-
equals
-
hashCode
-