The Claims interface in the JJWT library (io.jsonwebtoken.Claims) represents the payload of a JSON Web Token as a map of claim names to values. It extends Map<String, Object> and adds typed getters for the registered claims defined in RFC 7519, such as the subject, issuer, audience and expiration time.
You get a Claims object when you parse and verify a token, and you use it to read who the token is for, when it expires and which custom data it carries. This article uses the current JJWT 0.12.x API.
A JWT has three Base64URL-encoded parts: a header, a payload and a signature. The payload holds claims, which are statements about a subject plus extra data. RFC 7519 defines registered claim names:
| Claim | Meaning | Claims getter |
|---|---|---|
iss | Issuer | getIssuer() |
sub | Subject, usually the user id | getSubject() |
aud | Audience | getAudience() (a Set<String>) |
exp | Expiration time | getExpiration() |
nbf | Not before | getNotBefore() |
iat | Issued at | getIssuedAt() |
jti | Token id | getId() |
Anything else, such as roles or tenantId, is a custom (private) claim. The payload is only encoded, not encrypted, so never put passwords or secrets in it.
JJWT 0.12 split into an API artifact and runtime artifacts. With Maven:
Use the latest 0.12.x or newer release available to you. A HMAC key must be long enough for the algorithm: at least 256 bits for HS256. Generate or load a SecretKey rather than using a short string:
The fluent methods set registered claims, and claim(name, value) adds custom ones. Older tutorials use setSubject, setExpiration and signWith(SignatureAlgorithm.HS256, secret). Those forms are deprecated in 0.12, and the algorithm is now inferred from the key.
Parse with a verifying parser. Calling parseSignedClaims returns a Jws<Claims>, and getPayload() gives you the Claims.
The exact date in the output depends on when you run it. claims.get("roles", List.class) throws if the stored value cannot be converted, and claims.get("roles") returns the raw object or null. The parser checks exp and nbf automatically and throws ExpiredJwtException or PrematureJwtException, which are subclasses of JwtException.
A common pattern is to turn claims into an authenticated principal.
If your application only consumes tokens from an identity provider, Spring Security's resource server support (spring-boot-starter-oauth2-resource-server) parses and validates JWTs for you and exposes the claims as a Jwt object, so you may not need JJWT at all.
parseSignedClaims, never the unsigned parse methods, for tokens that matter.exp.iss and aud so a token meant for another service is rejected.It is an interface in io.jsonwebtoken that extends Map<String, Object>, so you can also iterate over all claims.
Call claim("name", value) on the builder, or claims().add(map), and read them back with claims.get("name", Type.class).
JJWT keeps the java.util.Date type for compatibility. Convert it with date.toInstant() for the java.time API.
JJWT 0.12 uses fluent methods such as subject() and claims(), and signWith(key). The older forms are deprecated.
Claims is JJWT's view of a JWT payload: a map with typed getters for the registered claims. Read it only after signature verification, validate issuer, audience and expiry, keep sensitive data out of it, and use the 0.12 builder and parser API instead of the deprecated methods.