What is a JWT
Sessions vs Tokens, Quickly
With form login, the server remembers you in an HTTP session and the browser sends a JSESSIONID cookie. A REST API used by a React app or a mobile app usually works differently: after login the server gives the client a token, and the client sends it on every request in the Authorization header. The server keeps nothing in memory. This is called stateless authentication.
1. POST /api/auth/login {email, password} ----------------> Spring Boot
checks password
2. <------------------------------------ 200 {"accessToken": "eyJ..."}
3. GET /api/orders
Authorization: Bearer eyJ... --------------------------> JWT filter verifies signature
and expiry, sets SecurityContext
4. <------------------------------------ 200 [ ...orders... ]The Three Parts
A JSON Web Token (JWT, pronounced "jot") is three Base64url strings joined with dots: header.payload.signature.
| Part | Contains | Example (decoded) |
|---|---|---|
| Header | Algorithm and type | {"alg":"HS256","typ":"JWT"} |
| Payload | Claims: facts about the user | {"sub":"[email protected]","roles":["ROLE_CUSTOMER"],"iat":1767225600,"exp":1767226500} |
| Signature | HMAC-SHA256(header + "." + payload, secret) | binary, Base64url encoded |
| Standard claim | Meaning |
|---|---|
sub | Subject: who the token is about (user id or email) |
iat | Issued at (seconds since 1970) |
exp | Expiry time; after this the token must be rejected |
iss, aud | Issuer (who made it) and audience (who it is for) |
jti | Unique token id (useful for revocation) |
Signed, Not Encrypted
Note: Anyone can decode the header and payload: Base64url is an encoding, not encryption. Paste a token into jwt.io and you can read it. Never put passwords, card numbers or Aadhaar numbers in a JWT. The signature only guarantees that nobody changed the token: if a user edits ROLE_CUSTOMER to ROLE_ADMIN, the signature no longer matches and the server rejects it.
| Algorithm | Type | Who can verify |
|---|---|---|
| HS256 | HMAC with one shared secret | Only servers that know the secret (also able to create tokens) |
| RS256 / ES256 | Private key signs, public key verifies | Anyone with the public key (used by OAuth2 servers, see advanced level) |