Security & Authentication

JSON Web Tokens (JWT) Demystified

H
HTMLtoPHP TeamMay 13, 2026 โ€ข 8 min read

If you have built or consumed a modern web API, used OAuth login with Google or GitHub, or managed user sessions in a Single Page Application (SPA), you have definitely encountered JSON Web Tokens (JWTs). They look like an intimidating string of random characters divided by two periods, but beneath the surface lies a remarkably elegant authentication architecture.

In this comprehensive guide, we will dissect the three anatomy parts of a JWT, explain how cryptographic signatures prevent user impersonation, and highlight the critical security mistakes developers make when implementing JWTs in production.

"A JWT is not encrypted by defaultโ€”it is digitally signed. Anyone can read the payload, but only your server can verify that the data was not tampered with."


1. What is a JSON Web Token?

A JSON Web Token (RFC 7519) is an open, industry-standard method for securely transmitting information between two parties as a compact, self-contained JSON object.

Because JWTs are self-contained, they hold all the required user information (such as user ID, email, role, and expiration timestamp). When a client sends a JWT in the Authorization: Bearer <token> HTTP header, the backend server can verify the user instantly without querying the database on every single API request.


2. The 3 Anatomy Parts of a JWT

A JWT consists of three separate strings concatenated by dots (xxxxx.yyyyy.zzzzz):

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTYiLCJuYW1lIjoiQWxleCIsImFkbWluIjp0cnVlLCJpYXQiOjE3MTU2MDAwMDB9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ
Part 1 (Red/Pink)

1. Header

Identifies the token type and the cryptographic hashing algorithm used (such as HMAC SHA256 or RSA):

{"alg": "HS256", "typ": "JWT"}
Part 2 (Purple)

2. Payload (Claims)

Contains the actual statements (claims) about the entity (user ID, username, roles, and expiration time exp):

{"sub": "123456", "name": "Alex", "admin": true, "exp": 1715603600}
Part 3 (Cyan)

3. Cryptographic Signature

Generated by hashing the encoded Header, encoded Payload, and a secret server key. If a malicious user alters "admin": true in the payload, the signature immediately fails validation.

HMACSHA256(base64UrlEncode(header) + "." + base64UrlEncode(payload), secretKey)

3. Why JWTs Use Base64URL Encoding

Standard Base64 encoding uses characters like + and /, as well as = padding at the end. In HTTP headers and query strings, these characters have special URI meanings and can corrupt transmission.

Base64URL solves this by replacing + with -, replacing / with _, and stripping out trailing = characters, ensuring tokens can be passed cleanly across HTTP headers and URLs without URL encoding issues.

๐Ÿ”
Inspect Tokens Safely

Need to debug an API token? Use our client-side JWT Decoder Tool. It decodes claims and timestamps in real-time in your browser without sending sensitive tokens to any third-party server.


4. 5 Critical JWT Security Best Practices

  1. Never store sensitive passwords or credit cards in payloads: Remember, anyone can decode a Base64 string in seconds. Only store non-sensitive identifiers (e.g. userId).
  2. Always set a short expiration time (exp): Access tokens should expire in 10-15 minutes. Use rotating Refresh Tokens for long sessions.
  3. Explicitly verify the algorithm on your server: Prevent the notorious "None Algorithm" attack by rejecting tokens where alg is set to none.
  4. Use strong, 256-bit signing secrets: A weak secret like "secret123" can be brute-forced by attackers using offline GPU hash-cracking tools in seconds.
  5. Store tokens in HttpOnly cookies when possible: Storing JWTs in browser localStorage makes them vulnerable to Cross-Site Scripting (XSS) attacks.

Frequently Asked Questions

Is a JWT encrypted?
Standard JWTs (JWS) are signed, not encrypted. Anyone who intercepts the token can read the header and payload. For encrypted payloads, you must use JWE (JSON Web Encryption).
How do I revoke or invalidate a JWT before it expires?
Because JWTs are stateless, you cannot revoke a token without maintaining an active blacklist (such as a Redis cache) of revoked token IDs (jti), or changing the user's secret key version in your database.

Conclusion

JWTs provide a standardized, highly scalable authentication mechanism for modern web apps when implemented with strong signatures, short lifetimes, and proper secret hygiene.

Decode and analyze JWT tokens in real-time.

Instantly inspect claims, headers, and expiry timestamps with our free browser utility.

Open JWT Decoder โ†’

More Guides (20)

View All โ†’
โœ‰๏ธ
Email & Productivity

How to Set Up Custom Domain Email in Outlook, Gmail & iPhone

8 min read
โ˜๏ธ
Networking & DNS

How to Set Up Cloudflare Free CDN & DNS: Speed, SSL & Protection

8 min read
๐Ÿ“ฌ
Security & Authentication

Why Are My Emails Going to Spam? How to Fix SPF, DKIM & DMARC

8 min read
๐Ÿ”“
Security & Authentication

How to Fix "Not Secure" Warning & SSL Mixed Content Errors

7 min read
๐Ÿš€
Networking & DNS

How to Connect a Custom Domain to GitHub Pages, Vercel & Netlify

8 min read
๐ŸŒ
Networking & DNS

The Ultimate Guide to DNS Records: A, AAAA, CNAME, MX, TXT & PTR

8 min read
๐Ÿ“ก
Networking & DNS

What Is an IP Address? Public vs. Private IPv4, IPv6 & Subnetting

7 min read
๐Ÿšฆ
Networking & DNS

HTTP Status Codes Explained: 200, 301, 404, 500 & 502 Bad Gateway

7 min read
๐Ÿ›ก๏ธ
Security & AuthenticationReading

JSON Web Tokens (JWT) Demystified: Header, Payload & Signatures

8 min read
โšก
Web Performance & SEO

Mastering Core Web Vitals in 2026: Optimize LCP, INP & CLS for SEO

8 min read
๐Ÿ”’
Security & Authentication

Is Your Website Actually Secure? 3 Network Checks You Must Run

6 min read
๐ŸŽจ
Developer & Data Tools

The CSS Color Nightmare: How to Manage HEX, RGB, and Gradients

6 min read
๐Ÿงน
Developer & Data Tools

Data Cleaning Guide: How to Clean JSON, CSV & Text Like a Pro

7 min read
๐Ÿ”—
Content & Social

Stop Sharing Ugly Links: The Complete Guide to Open Graph & Twitter Cards

4 min read
๐Ÿ“‰
Web Performance & SEO

Why You Must Minify HTML, CSS, and JS Before Every Deployment

4 min read
๐Ÿ–ผ๏ธ
Web Performance & SEO

Stop Using PNGs for Everything: The Developer Guide to Image Formats

5 min read
๐Ÿš€
Web Performance & SEO

5 Essential Steps to Optimize Your Website Before Launch

4 min read
๐ŸŽฏ
Web Performance & SEO

Why Your Website is Invisible on Google: The Ultimate Meta Tag Checklist

5 min read
๐Ÿ”—
Developer & Data Tools

Stop Breaking Your Links: The Developer Guide to URL Encoding and Base64

5 min read
๐Ÿ“
Content & Social

Stop Writing HTML by Hand: The Ultimate Guide to Markdown and Text Tools

5 min read
โšก

Explore 70+ Free Tools

Instant client-side developer, networking, SEO & formatting utilities.

Browse All Tools โ†’