Oct
9

What Is a JWT? JSON Web Tokens Explained

10/09/2026 02:28 PM bởi Seovaro Team trong


What Is a JWT? JSON Web Tokens Explained

Log in to almost any modern web app and a JSON Web Token is doing the quiet work of proving who you are. JWTs are the standard way for services to pass authentication and identity claims between parties — compact, self-contained, and verifiable without a database lookup. If you have a token and want to see what is inside it, paste it into our free JWT decoder to inspect its header, payload, and signature.

This guide explains the three-part structure of a JWT, how base64url encoding works, the standard claims you will see in the payload, how signature verification actually proves authenticity, where JWTs get used in real systems, and the security mistakes that turn them into vulnerabilities.

JWT Structure: Header, Payload, Signature

A JWT is three base64url-encoded segments joined by dots:

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9
.
eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ
.
SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c

The three parts:

  • Header — JSON describing the token: the signing algorithm (alg, e.g. HS256 or RS256) and the token type (typ: JWT).
  • Payload — JSON containing the claims: statements about the user and the token itself, like who it was issued for and when it expires.
  • Signature — the cryptographic proof. Created by signing the encoded header and payload with a secret (HMAC) or private key (RSA/ECDSA). This is what stops anyone from tampering with the first two parts.

Decoded, the example above reveals:

// Header
{ "alg": "HS256", "typ": "JWT" }

// Payload
{ "sub": "1234567890", "name": "John Doe", "iat": 1516239022 }

Base64url Encoding: Why Tokens Look Like Gibberish

The header and payload are JSON, but they are encoded with base64url — a URL-safe variant of base64 that replaces + with -, / with _, and omits the = padding. This makes the token safe to put in URLs, HTTP headers, and cookies without escaping.

Critical point that trips up beginners: base64url is encoding, not encryption. Anyone who holds the token can decode the payload in seconds — that is by design, and it is exactly what our JWT decoder does. Never put secrets, passwords, or sensitive personal data in the payload. The payload is signed for integrity, not encrypted for confidentiality. (There is a separate standard, JWE, for encrypted tokens — but a plain JWT is an open book.)

Standard Claims You Will Encounter

Claims are just name/value pairs, but a set of registered names have agreed meanings:

  • iss (issuer) — who created the token, e.g. "https://auth.example.com".
  • sub (subject) — who the token is about, usually a user ID. This is the claim your application logic keys on.
  • aud (audience) — who the token is intended for. A service should reject tokens not addressed to it.
  • exp (expiration) — Unix timestamp after which the token must be rejected. The single most important claim for security.
  • iat (issued at) — when the token was created.
  • nbf (not before) — the token is invalid before this time. Useful for scheduled access.
  • jti (JWT ID) — a unique identifier for the token, used to track or revoke individual tokens.

On top of these, applications add custom claims: role: "admin", plan: "pro", email, and so on. Custom claims are convenient — the service reading the token gets user attributes without a database query — but every claim you add makes the token bigger and every byte travels with each request.

How Verification Actually Works

The elegance of JWTs is that verification needs no database. The receiving server:

  1. Takes the encoded header and payload from the token.
  2. Recomputes the signature using its secret key (HS256) or the issuer's public key (RS256).
  3. Compares the recomputed signature with the signature in the token. If they match, the content has not been altered since signing.
  4. Checks the claims: is exp in the future, is aud correct, is iss trusted?

This is why JWTs scale so well for microservices: any service holding the public key (or shared secret) can verify tokens independently. No central session store, no lookup per request. The trade-off is revocation — a validly signed token stays valid until it expires, which is why short expiration times matter.

Where JWTs Are Used

  • API authentication. The client sends the JWT in the Authorization: Bearer header with each request. Stateless and simple.
  • Single sign-on (SSO). OpenID Connect issues ID tokens as JWTs, letting one login work across many applications.
  • Service-to-service auth. Microservices pass JWTs to prove identity to each other without sharing session state.
  • Password reset and email verification links. A short-lived signed token in the URL proves the link is genuine without storing anything server-side.
  • Information exchange. Any scenario where one party asserts claims another party needs to trust — with the signature as the trust mechanism.

Security Notes: How JWTs Go Wrong

JWTs are safe when used correctly and dangerous when misunderstood. The classic failures:

  • Secrets in the payload. Worth repeating: the payload is readable by anyone. Passwords, API keys, and personal data do not belong there — ever.
  • Long or missing expiration. A token valid for 30 days is a 30-day window of exposure if leaked. Access tokens should live minutes to hours; use refresh tokens for long sessions.
  • The alg: none attack. Some libraries historically accepted tokens with the algorithm set to none (no signature). Modern libraries reject this, but the lesson stands: always validate the algorithm against an allowlist, never trust the token's own header.
  • Weak HMAC secrets. HS256 is only as strong as the shared secret. A short or guessable secret can be brute-forced offline, letting an attacker forge arbitrary tokens. Use long random secrets; prefer RS256 (asymmetric) when many services verify but only one signs.
  • Storing tokens in localStorage. Any JavaScript on the page — including injected malicious scripts — can read localStorage. HttpOnly cookies are the safer home for tokens in browsers, trading some convenience for XSS resistance.
  • Skipping claim validation. Verifying the signature but ignoring exp, aud, and iss is like checking an ID's hologram but not its expiry date.

Final Thoughts

A JWT is a signed envelope for claims: header says how it was sealed, payload says what is asserted, signature proves nobody altered it. That simple design powers authentication across the modern web. Use short expirations, validate every claim, keep secrets out of the payload, and store tokens where scripts cannot reach them.

Have a token you want to inspect? Paste it into our free JWT decoder to see its header, payload claims, and expiration at a glance.

FAQ

Are JWTs encrypted?

No. A standard JWT (JWS) is signed but not encrypted — the payload is base64url-encoded and readable by anyone holding the token. For confidentiality you need JWE (JSON Web Encryption), a separate standard.

What is the difference between HS256 and RS256?

HS256 uses one shared secret for both signing and verification (symmetric). RS256 uses a private key to sign and a public key to verify (asymmetric), so many services can verify tokens without being able to forge them. RS256 is preferred when the signer and verifiers are different parties.

How long should a JWT last?

Access tokens: minutes to a few hours. Long-lived sessions should use short access tokens plus rotating refresh tokens, so a leaked access token has a small blast radius.

Can a JWT be revoked before it expires?

Not inherently — that is the trade-off of statelessness. Common workarounds include very short expirations, a denylist of token IDs (jti) checked on sensitive operations, or rotating the signing key (which invalidates everything).