A stamped wristband
At a festival, the ticket office puts a wristband on you. It has three parts: a note on which kind of stamp is used, a printed strip saying who you are, which areas you can enter and until when, and a stamp made with a secret only the office has.
Anyone can read the printed strip. What they can't do is change it without the stamp giving them away.
Header, payload, signature
A JWT is three base64url-encoded parts joined by dots: header.payload.signature.
1{ "alg": "RS256", "typ": "JWT" }2{3"sub": "42",4"iss": "https://auth.shop.com",5"aud": "orders-api",6"exp": 1760000000,7"role": "user"8}
- The header names the signing algorithm.
- The payload carries claims: registered ones like
sub,iss,aud,exp,iat, plus your own. - The signature covers the first two parts, using an HMAC secret (
HS256) or a private key (RS256,ES256).
The payload is encoded, not encrypted. Never put secrets in it.
Checking at the gate
Every ride and food tent has a guide to the office's stamp. They check your band right there, with no phone call to the office, so even huge crowds move quickly.
Stateless verification
The issuer signs with a private key and publishes the public key at a JWKS endpoint. Each API fetches it once, caches it, and verifies every incoming token locally:
- Check the signature, using the algorithm you expect, never just whatever the header says.
- Check
exp(andnbf), allowing a little clock skew. - Check
issandaud, so a token meant for another service is rejected.
No session table and no network call per request, so any number of services can verify tokens independently.
Scribbles and old bands
If someone scribbles "VIP" over the printed strip, the stamp no longer matches and the gate says no. But a real band can't be taken back: if it's valid until tonight, it works until tonight. So the office only gives out bands that last a short while.
Tampering, expiry and revocation
Changing any byte of the header or payload invalidates the signature, and without the key an attacker can't
produce a new one. Classic mistakes are accepting alg: none, or letting an attacker switch RS256 to HS256
so the public key is used as an HMAC secret. Use a library and pin the algorithm.
The flip side of stateless checks: there's no central place to revoke a token. Keep access tokens short-lived
(minutes) and pair them with refresh tokens, or keep a deny-list of token ids (jti) for emergencies, which
brings back some of the lookups JWTs avoided.
In Spring: spring-boot-starter-oauth2-resource-server with spring.security.oauth2.resourceserver.jwt.issuer-uri
set fetches the JWKS and validates signature, expiry and issuer for every request.
JWT versus sessions: a session id in a cookie is simple and instantly revocable, and is often the better choice for a single web app. JWTs shine when many services must verify identity without a shared session store.