Class ClaimsPrincipal
- All Implemented Interfaces:
Principal
Principal that exposes typed read-only access to a map of claims.
Used by token validators that produce structured assertions about the authenticated subject —
e.g. JWTs (the juneau-rest-server-auth-jwt JwtTokenValidator returns a
ClaimsPrincipal with the verified token's claim set) or OAuth 2.0 introspection responses.
The "principal name" returned by getName() is supplied at construction time; callers can
pick whichever claim is canonical for their identity model (typically sub). The full claim
set is reachable via getClaims() and per-claim typed lookup via
getClaim(String, Class).
Instances are immutable — the claims map supplied to the constructor is defensively
copied and exposed as an unmodifiable view. ClaimsPrincipal can be safely cached and
shared across requests.
Type coercion contract for getClaim(String, Class):
- If the underlying value is already an instance of the requested type, it is returned directly.
- If the requested type is
String, the value'sObject.toString()is returned. Numbersub-types (Integer,Long,Double) coerce via the matchingNumber.xxxValue()accessor; non-numeric values yieldOptional.empty().Booleancoerces fromBooleanonly.- Any other mismatch yields
Optional.empty()rather than a cast exception.
The coercion table is intentionally conservative — the validator that built the claims map is
the authoritative source of richer type-mapping. If a JWT validator decodes a claim as a
List<String>, callers can read it back as List.class or pull individual
elements via getClaims() and operate on the List directly.
See Also:
- Since:
- 10.0.0
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
ClaimsPrincipal
Constructor.- Parameters:
name- The principal name. Must not benull or blank.claims- The claim map. May benull (treated as an empty map). Defensively copied.
-
-
Method Details
-
getName
-
getClaims
Returns an unmodifiable view of the full claim set.- Returns:
- The full claim map. Never
null .
-
getClaim
Returns the value of a claim by name with the supplied target type.See the class-level Javadoc for the coercion contract.
- Type Parameters:
T- The target type.- Parameters:
claimName- The claim name. Must not benull .type- The target type. Must not benull .- Returns:
- The coerced value, or
Optional.empty()if the claim is absent or cannot be coerced.
-
hasClaim
Returnstrue if a claim with the supplied name exists in the claim set.- Parameters:
claimName- The claim name. Must not benull .- Returns:
true if the claim is present, even if its value is the JSONnullliteral.
-
toString
-