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.

PartContainsExample (decoded)
HeaderAlgorithm and type{"alg":"HS256","typ":"JWT"}
PayloadClaims: facts about the user{"sub":"[email protected]","roles":["ROLE_CUSTOMER"],"iat":1767225600,"exp":1767226500}
SignatureHMAC-SHA256(header + "." + payload, secret)binary, Base64url encoded
Standard claimMeaning
subSubject: who the token is about (user id or email)
iatIssued at (seconds since 1970)
expExpiry time; after this the token must be rejected
iss, audIssuer (who made it) and audience (who it is for)
jtiUnique 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.

AlgorithmTypeWho can verify
HS256HMAC with one shared secretOnly servers that know the secret (also able to create tokens)
RS256 / ES256Private key signs, public key verifiesAnyone with the public key (used by OAuth2 servers, see advanced level)