JWT Tokens Explained: Structure, Security, Best Practices
JSON Web Tokens show up in almost every modern authentication system. That long string in your Authorization header β eyJhbGciOiJIUzI1NiIs... β is a JWT, and understanding what is inside it and how it works is essential for building secure applications.
What Is a JWT?
A JWT is a compact, URL-safe token format that carries claims (data) between two parties. It is self-contained β the token itself holds the information needed to verify its authenticity and extract user data, without requiring a database lookup.
Decode any JWT instantly with our JWT Encoder/Decoder. Paste a token and see its header, payload, and signature broken down.
JWT Structure: Three Parts
Every JWT has three Base64URL-encoded parts separated by dots:
header.payload.signature
Header
{
"alg": "HS256",
"typ": "JWT"
}
Specifies the signing algorithm and token type.
Payload
{
"sub": "user123",
"name": "Alex Chen",
"role": "admin",
"iat": 1708963200,
"exp": 1709049600
}
Contains claims β the actual data. Standard claims include sub (subject), iat (issued at), and exp (expiration).
Signature
Created by signing the encoded header and payload with a secret key. This prevents tampering β any modification invalidates the signature.
How JWT Authentication Works
- User logs in with credentials
- Server creates a JWT with user claims and signs it
- Server returns the JWT to the client
- Client sends JWT in the Authorization header for subsequent requests
- Server verifies the signature and extracts claims β no database query needed
Security Best Practices
Always Verify the Signature
Never trust a JWT without verifying its signature. Decode the payload with our JWT Decoder to inspect tokens during development, but always verify cryptographically in production.
Use Short Expiration Times
JWTs cannot be revoked once issued (unlike session tokens). Keep expiration times short (15-60 minutes) and use refresh tokens for longer sessions.
Choose the Right Algorithm
- HS256 β HMAC with SHA-256, symmetric key. Simple, fast, good for single-server apps
- RS256 β RSA with SHA-256, asymmetric keys. Better for distributed systems where multiple services verify tokens
- Never use "none" β The
alg: "none"vulnerability has caused real-world breaches
Protect the Secret Key
Your JWT signing key is the master key to your authentication system. Store it securely, rotate it periodically, and never expose it in client-side code.
Do Not Store Sensitive Data in the Payload
JWT payloads are Base64-encoded, not encrypted. Anyone can decode them. Never include passwords, credit card numbers, or secrets.
Common JWT Mistakes
- Not validating expiration β Always check the
expclaim - Storing JWTs in localStorage β Vulnerable to XSS attacks. Use httpOnly cookies instead
- Oversized tokens β JWTs go in every request header. Keep payloads lean
- Not using HTTPS β JWTs sent over HTTP can be intercepted
Frequently Asked Questions
Can I revoke a JWT?
Not directly β JWTs are stateless. Workarounds include short expiration times, token blacklists, or changing the signing key (which invalidates all tokens).
Should I use JWTs for sessions?
JWTs work well for API authentication and microservices. For traditional web sessions, server-side sessions with cookies are often simpler and more secure.
How is JWT different from OAuth?
OAuth is an authorization framework. JWT is a token format. OAuth can use JWTs as access tokens, but they solve different problems.
Related Resources
- Base64 Encoding Explained β understand the encoding JWTs use
- Understanding JWT Security β deeper dive into vulnerabilities
- JWT Encoder/Decoder β decode and create JWTs instantly
- Hash Generator β explore the algorithms behind JWT signatures