JWT Best Practices: signing algorithms (RS256 over HS256 for multi-service), key rotation via kid header and JWKS, short-lived access + rotating refresh tokens, audience and exp/nbf validation, jti for revocation via Redis blocklist, alg-confusion attacks (alg=none, RS256->HS256 swap), and storage trade-offs (HttpOnly cookies vs localStorage XSS). 5 animated scenarios plus an embedded ADR comparing JWT vs opaque tokens with introspection.
A JWT is a token format, not an authentication or authorization decision by itself. Each token kind has an exclusive validation profile, trusted key source, intended audience, lifetime, and replay or revocation policy.
| Component | Responsibility |
|---|---|
browser | Browser |
client | Client or Backend for Frontend |
issuer | Trusted Token Issuer |
jwks | Pinned Issuer JWKS |
gateway | JWT Validation Gateway |
api | Protected API |
revocation-state | Session and Revocation State |
refresh-service | Refresh Token Family Store |
audit-log | Validation Audit |
attacker | Token Thief |
Topology edges represent authenticated or otherwise explicit communication paths. Responses reuse those physical paths in reverse; no response-only or bypass edges are added.
issue-and-validate — Issue and validate under one token profileThe verifier fixes the expected token type and algorithm set, resolves keys from the configured issuer, validates required claims, then applies current authorization state.
algorithm-confusion-rejection — Reject algorithm confusionThe verifier uses a server-configured algorithm allowlist and a key bound to that algorithm; the token header cannot select none or switch asymmetric and symmetric verification.
issuer-audience-time — Validate issuer, audience, and time claimsA valid signature is insufficient when issuer, audience, expiry, not-before, or required token-profile claims do not match.
cross-jwt-confusion — Keep token kinds mutually exclusiveID Tokens, access tokens, logout tokens, and internal session tokens use distinct typ values, claims, keys or audiences so one kind cannot substitute for another.
signing-key-rotation — Rotate signing keys with bounded overlapThe issuer publishes a new trusted key before use, retains the old verification key only while unexpired tokens require it, then removes it according to policy.
untrusted-key-url-rejection — Reject token-controlled key locationsA jku or x5u header is untrusted input. The verifier never lets the token choose a network destination; kid can only select within the issuer key set configured by trusted metadata.
browser-token-theft — Model browser token exposureJWT payloads are merely encoded when signed. Browser storage choice changes exposure, while XSS can still perform same-origin actions even when an HttpOnly cookie hides the session value.
authorization-freshness — Short lifetime does not replace authorizationA token that has not expired may outlive a role removal; sensitive APIs combine token validation with current session or authorization state.
refresh-family-reuse — Rotate refresh tokens and detect reuseA refresh token is a high-value credential stored separately from access tokens; reuse of an invalidated family member triggers containment.
Введите числа или выберите пресет