TwiceToldby Develoker
ConceptsSystem designLabsCheat sheetsAbout

Develoker makes free tools for learning to build software. TwiceTold explains each idea twice, as an everyday analogy and the real thing; CloudArch lets you design cloud systems and put load on them.

About ยท Labs ยท Cheat sheets ยท Learning paths

All concepts ยท web ยท security ยท auth

JWT

JSON Web Tokens are compact, signed bundles of claims. Any service with the key can verify one on the spot, without a database lookup, which makes them great for APIs and hard to revoke.

10 min3 animations

๐Ÿ“š Builds on๐Ÿ“ฎ HTTP

๐Ÿ–จ๏ธ Cheat sheet

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.

Three parts and two dots3 steps
Wristband012๐ŸŽชTicket office
Press play and watch it happen!

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:

  1. Check the signature, using the algorithm you expect, never just whatever the header says.
  2. Check exp (and nbf), allowing a little clock skew.
  3. Check iss and aud, 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.

Checking the wristband at the gate3 steps
๐Ÿง’You๐ŸŽชTicket office๐ŸŽกRide gate๐Ÿ”Food tent
Press play and watch it happen!

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.

Tampering and expiry3 steps
๐Ÿง’You๐ŸŽคVIP gate
Press play and watch it happen!
๐Ÿ”ง Going deeper

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.

๐Ÿงฉ Quick quiz

Quick quiz! 3 questions about JWT, told the same way as the story.
  1. Anyone can read the wristband's printed strip. Why can't they change it?
  2. Why can every gate check bands without phoning the ticket office?
  3. A real band can't be taken back. What does the office do about that?
Answer every question to check.Passing marks JWT as done.
Got the hang of JWT? Mark it as done, and your paths show how far youโ€™ve got.

Saved in this browser.

๐Ÿ›ก๏ธ Signing in safelyStep 4 of 5

โ† Previous on the path๐Ÿšง CORSNext on the path โ†’๐Ÿ—๏ธ OAuth 2.0

๐Ÿ‘‰ Read next

  • ๐Ÿ—๏ธOAuth 2.0A hotel desk gives your helper a key card for one room, for an hour, instead of your master key.
  • ๐Ÿ›Ž๏ธAmazon API GatewayThe service counter of a big store: it checks each shopper's card, keeps the queue moving and points them on.