JWT Structure

A JWT is three Base64URL-encoded parts separated by dots:

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9 . eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4iLCJpYXQiOjE1MTYyMzkwMjJ9 . SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c

1. Header (red)

Decoded header { "alg": "HS256", // signing algorithm "typ": "JWT" }

2. Payload (green)

Decoded payload — standard claims + custom { "sub": "1234567890", // subject (user ID) "name": "John Doe", // custom claim "role": "admin", // custom claim "iat": 1516239022, // issued at (Unix timestamp) "exp": 1516242622 // expiry (Unix timestamp) }

3. Signature (blue)

How the signature is created HMACSHA256( base64UrlEncode(header) + "." + base64UrlEncode(payload), secretKey )

The signature ensures the header and payload were not modified. If any bit of the header or payload changes, the signature becomes invalid.

⚠️ Payloads Are NOT Encrypted

Base64URL encoding is not encryption — anyone can decode a JWT and read the payload. Never put passwords, credit card numbers, or sensitive PII in a JWT payload. Use JWE (JSON Web Encryption) if you need to encrypt the payload.

How JWT Authentication Works

  1. User logs in: Client sends credentials (username + password) to the server.
  2. Server issues JWT: Server verifies credentials, creates a JWT with user ID and claims, signs it with the secret key, returns it to the client.
  3. Client stores JWT: Typically in memory, localStorage, or an HttpOnly cookie.
  4. Client sends JWT with requests: Every API request includes the JWT in the Authorization: Bearer <token> header.
  5. Server verifies JWT: Server recomputes the expected signature. If it matches, it trusts the claims — no database lookup needed. If it does not match or the token is expired, the request is rejected.

JWT vs Session Authentication

FeatureJWT (Stateless)Session (Stateful)
Server storageNone — token is self-containedSession stored in DB/Redis
ScalabilityExcellent — any server can verifyRequires shared session store
RevocationHard — must use blocklist or short TTLEasy — delete session from store
Token size~200–500 bytes (sent every request)Small session ID cookie (~20 bytes)
Best forMicroservices, APIs, mobile appsTraditional web apps, monoliths
LogoutComplex (need blocklist)Trivial (delete session)

HS256 vs RS256 vs ES256

AlgorithmTypeKeyUse When
HS256Symmetric (HMAC)Single shared secretSingle service — only one verifier
RS256Asymmetric (RSA)Private key signs, public key verifiesMultiple services need to verify tokens
ES256Asymmetric (ECDSA)Smaller key, same security as RS256Mobile/IoT where key size matters

Standard JWT Claims

ClaimFull NamePurpose
issIssuerWho issued the token (e.g. "api.example.com")
subSubjectWho the token is about (user ID)
audAudienceWho should accept the token
expExpirationUnix timestamp when token expires
iatIssued AtUnix timestamp when token was created
jtiJWT IDUnique ID — used for token blocklisting
nbfNot BeforeToken not valid before this timestamp

JWT Security Best Practices

  • Always verify the signature — never trust a JWT without verifying it
  • Set short expiry — 15 minutes for access tokens; use refresh tokens for longer sessions
  • Use RS256 for multi-service architectures — never share your signing private key
  • Store in HttpOnly cookies — prevents XSS from reading the token
  • Validate the aud claim — ensures the token was issued for your service specifically
  • Use jti for revocation — store revoked JTIs in Redis for logout functionality
  • Never put sensitive data in payload — payload is readable by anyone who has the token

💡 Decode Any JWT Instantly

A JWT's payload is just Base64URL-encoded JSON. You can decode it with any Base64 decoder. For debugging, paste your JWT into jwt.io (use only non-sensitive tokens — never paste production tokens into third-party tools).

How We Research and Update This Guide

We test the underlying formula or workflow, compare outputs with reliable references, and revise examples whenever the page content changes.

  • The workflow or formula is tested directly in the tool and compared against independent reference examples.
  • Examples are kept practical so readers can verify the result without hidden assumptions.
  • Pages are revised whenever the interface, calculation flow, or surrounding guidance materially changes.

Frequently Asked Questions — JWT