JSON Web Tokens (JWTs) are compact and convenient, but mistakes in signing, storage, or validation can lead to account takeover. This guide explains how JWTs work, common pitfalls, and a secure blueprint for production deployments.
A JWT has three Base64URL-encoded parts: header.payload.signature. The header defines the algorithm, the payload holds claims, and the signature binds them together.
none and weak/legacy algs. Disable algorithm downgrades server-side.iat) and not-before (nbf) claims to prevent early or replayed use.exp, nbf, iss, aud, and sub against expected values.kid against a whitelist; avoid direct filesystem access based on kid.kid, exp) not the full token.jwt-decoder tool to inspect headers/claims safely without relying on untrusted libraries.iss, aud, exp, nbf, sub)jwt-decoder during development to verify claim sets before rollout.JWT (JSON Web Token) is a compact, URL-safe token format defined by RFC 7519. It consists of three Base64URL-encoded parts: header, payload, and signature, separated by dots.
RS256 uses asymmetric cryptography (public/private key pair), allowing you to verify tokens without exposing the signing key. HS256 uses a shared secret, which must be kept secure by all parties. RS256 is preferred for distributed systems and better key management.
Keep access tokens short-lived (5-30 minutes) to limit exposure if compromised. Use refresh tokens for longer sessions, rotating them on each use and detecting reuse attempts.
No, avoid localStorage for JWTs. Use httpOnly, secure cookies with SameSite=Lax/Strict instead. This prevents XSS attacks from stealing tokens, as JavaScript cannot access httpOnly cookies.
Algorithm confusion occurs when an attacker changes the algorithm in the JWT header (e.g., from RS256 to HS256) and uses the public key as the HMAC secret. Prevent this by hardcoding accepted algorithms and ignoring the header-supplied algorithm during verification.
JWTs are stateless, so revocation requires additional mechanisms: maintain a blocklist of revoked token IDs, use short expiration times, rotate signing keys, and invalidate refresh token chains on suspicion.
Yes, JWTs can be decoded client-side (the payload is Base64URL-encoded, not encrypted). However, always verify the signature server-side. Use our JWT Decoder tool to inspect tokens safely during development.