Comprehensive Guide to JSON Web Tokens (JWT): Anatomy, Security & Verification
Standard Reference: IETF RFC 7519 (JSON Web Token) & RFC 7515 (JSON Web Signature)
The Tripartite Anatomy of a JSON Web Token
A standard JSON Web Token consists of three Base64URL-encoded strings concatenated by periods (header.payload.signature):
Specifies token type ("typ": "JWT") and the
cryptographic signing algorithm (e.g., "alg": "HS256" or "RS256").
Encapsulates registered statements (sub, iss, exp, iat) alongside application-specific user permissions and roles.
Calculated by hashing Base64URL(header) + "." + Base64URL(payload) with the secret key to guarantee message integrity.
Critical Security Best Practices & Vulnerability Prevention
🛡️ The "alg": "none" Exploit
Flawed verification libraries may accept tokens where the header specifies "alg": "none", treating un-signed tokens as verified! Always hardcode permitted algorithms (e.g.
whitelist strictly ['HS256']) on your backend
verification handlers.
🔒 HttpOnly Cookies vs LocalStorage
Storing JWTs in browser localStorage exposes sessions
to Cross-Site Scripting (XSS) credential theft. Always store sensitive bearer JWTs in HttpOnly, Secure, SameSite=Strict cookies.