Sub topic 4
Reading the claims that matter
This chapter assumes you know the three parts of a JWT (header, payload, signature) and have a Spring Boot resource server running. Here you will learn which payload fields Spring Security uses and how to read them in code.
What a claim is
A claim is one key-value pair in the JWT payload. The JWT specification (RFC 7519) defines a short list of registered claims with fixed meanings. Spring Security checks some of them for you and exposes all of them on the Jwt object.
{
"iss": "https://issuer.example.com/realms/demo",
"sub": "user-42",
"aud": ["orders-api"],
"exp": 1790000000,
"nbf": 1789996400,
"iat": 1789996400,
"scope": "orders.read orders.write"
}The registered claims
| Claim | Meaning | Type in the token | Checked by default? |
|---|---|---|---|
| `iss` | Who issued the token | String (URL) | Only if you configure `issuer-uri` |
| `sub` | Who the token is about | String | No |
| `aud` | Who the token is for | String or array of strings | No |
| `exp` | Time after which the token is invalid | Seconds since the epoch | Yes |
| `nbf` | Time before which the token is invalid | Seconds since the epoch | Yes, if present |
| `iat` | Time the token was issued | Seconds since the epoch | No |
Time claims are numbers of seconds, not milliseconds. Spring converts them to java.time.Instant for you.
The default timestamp check allows 60 seconds of clock skew. A token that expired 30 seconds ago is still accepted.
Reading claims in a controller
Spring Security places the validated token in the authentication as a Jwt. Inject it with @AuthenticationPrincipal.
import java.time.Instant;
import java.util.List;
import java.util.Map;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.oauth2.jwt.Jwt;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
class ClaimsController {
@GetMapping("/claims")
Map<String, Object> claims(@AuthenticationPrincipal Jwt jwt) {
String subject = jwt.getSubject();
List<String> audience = jwt.getAudience();
Instant expiresAt = jwt.getExpiresAt();
String scope = jwt.getClaimAsString("scope");
return Map.of(
"sub", subject,
"aud", audience,
"exp", expiresAt.toString(),
"scope", scope == null ? "" : scope);
}
}getAudience() always returns a list, even when the token carries aud as a single string. Typed getters such as getIssuedAt() can return null when the claim is absent, so check before calling methods on the result. getExpiresAt() is safe here only because the default validator rejects tokens without a usable exp. For custom claims, use getClaimAsString, getClaimAsStringList, or getClaims(), which returns the whole map.
Which claim to trust for what
- Use
subas the stable identifier for a user. It is unique within one issuer, so pair it withissif you accept several issuers. - Do not use
iatfor security decisions. It records when the token was issued. Rely onexpandnbffor validity. - Treat custom claims such as
emailorrolesas input from the issuer. They are trustworthy only because the signature was verified.
Exercise
A token has "aud": "billing-api" and your service is orders-api. It was signed by your trusted issuer and has not expired. With only the issuer-uri property set, does Spring Security accept it, and what does jwt.getAudience() return?
Show answer
Spring accepts it, because the default validators check the signature, timestamps, and issuer but not aud. getAudience() returns a list containing the single value billing-api. To reject the token you must add an audience validator to the JwtDecoder.