A JSON Web Token (JWT) is a compact, signed token that carries claims about a user or client, so a server can verify who is calling and what they may do without looking up a session. Its significance in security is stateless, verifiable authentication across services, but it is also easy to misuse: a JWT is signed, not encrypted by default, and cannot be revoked simply by deleting server state.
JWTs are defined in RFC 7519, with signing in the JWS standard (RFC 7515). Best-practice guidance is collected in RFC 8725. They are the standard format for OpenID Connect ID tokens and are widely used for OAuth 2.0 access tokens.
A JWT is three Base64URL-encoded parts separated by dots: header, payload and signature.
Decoded, the header and payload look like this:
The registered claims you will see most are iss (issuer), sub (subject), aud (audience), exp (expiry), nbf (not before), iat (issued at) and jti (token id). Anyone holding the token can decode and read the payload, so never put secrets or sensitive personal data in it.
exp claim limits how long a stolen token is useful.| Risk | Cause | Mitigation |
|---|---|---|
| Accepting unsigned or downgraded tokens | The server trusts the alg header, including none | Pin the expected algorithm and key in the verifier |
| Algorithm confusion | An RS256 public key is used as an HS256 secret | Configure one algorithm per key, never choose it from the token |
| Missing claim checks | Only the signature is verified | Always validate iss, aud, exp and nbf |
| Token theft | Token stored where scripts can read it, or sent over HTTP | Use HTTPS, short lifetimes, and avoid localStorage for sensitive tokens |
| No revocation | Valid until exp | Short access tokens, refresh token rotation, and a deny list for emergencies |
| Weak secrets | Short HS256 secrets can be brute forced | Use long random secrets or asymmetric keys |
A signed JWT (JWS) is readable by anyone. If the content must be confidential, use an encrypted token (JWE) or keep the data on the server and put only an opaque reference in the token.
Do not write signature checking yourself. Use a maintained library or framework. In Spring Security 6 a resource server validates bearer tokens against the issuer's keys with a few lines:
Spring discovers the JWKS URL from the issuer, verifies the signature, checks expiry and issuer, and maps the scope claim to SCOPE_ authorities. Add an audience validator if your identity provider issues tokens for several APIs.
A secure design pairs a short-lived access token (minutes) with a longer-lived refresh token that is stored more carefully and rotated on use. In browsers, an HttpOnly, Secure, SameSite cookie keeps the token away from JavaScript, which reduces damage from XSS, though cookies require CSRF protection. For single-page apps, many teams use a backend-for-frontend that holds tokens server-side and gives the browser only a session cookie.
For service-to-service calls, use the OAuth 2.0 client credentials flow and cache tokens until shortly before expiry.
| Aspect | JWT | Server session |
|---|---|---|
| State on server | None required | Session store required |
| Revocation | Hard until expiry | Immediate |
| Scaling across services | Easy | Needs shared store |
| Size per request | Larger header | Small cookie id |
| Best for | APIs, microservices, delegated access | Traditional web apps with one backend |
JWTs are not automatically better. If you have a single web application with a session store, plain sessions are simpler and easier to revoke.
Not by default. A standard signed JWT is only Base64URL encoded, so anyone can read its claims. Use JWE if the payload must be confidential.
You cannot unsign it. Keep lifetimes short, rotate refresh tokens, and keep a deny list keyed by jti or by user for urgent revocation.
Prefer asymmetric algorithms (RS256, ES256 or EdDSA) when several services verify tokens, and fix the algorithm on the verifier. HS256 is acceptable when issuer and verifier are the same trusted service with a strong secret.
An HttpOnly secure cookie is generally safer than localStorage, since scripts cannot read it. Pair it with CSRF protection.
JWTs give APIs a compact, signed way to carry identity and permissions, enabling stateless validation and single sign-on. Their risks are well known: trust only the algorithm you configure, validate every relevant claim, keep tokens short-lived and never put secrets in the payload. Use a vetted library, and choose sessions when you need simple revocation.