Back to Articles

· 5 min read · views

Secure JWT Best Practices That Actually Hold Up

JWTs are compact and easy to misuse. A secure setup is less about clever claims and more about algorithm discipline, short lifetimes, honest audience checks, and treating the token like a bearer credential — because that is what it is.

Secure JWT Best Practices That Actually Hold Up

JSON Web Tokens feel simple: three Base64url segments, a signature, done. That simplicity is why teams ship them everywhere — and why the same five mistakes keep showing up in incident reports: alg: none, oversized lifetimes, secrets in repos, skipped aud/iss checks, and “we store the JWT in localStorage so the SPA is easier.”

A secure JWT design is boring on purpose. Ideal means predictable verification, minimal claims, and a clear plan for rotation and revocation.

Decide what the token is allowed to prove

Before algorithms and libraries, write the rule in one sentence: this token proves X for audience Y until time Z.

Good fits:

  • Short-lived access tokens for APIs
  • Service-to-service assertions with tight aud
  • Session continuation when paired with a server-side session or refresh design

Poor fits:

  • “Forever login” without refresh/rotation
  • Stuffing authorization databases into the payload
  • Treating an unverified JWT as a trusted user object

If you need to revoke access in seconds, a pure self-contained JWT without a denylist/session store is the wrong primary control. Pair short TTL with a revocation strategy you can operate.

Abstract JWT lifecycle with rotating signing key Ideal JWT ops: short-lived access, rotatable keys, verification that rejects surprises

Algorithm discipline (non-negotiable)

Pick one modern family and reject everything else at verify time.

Use case Prefer Avoid
Single service / shared secret HS256+ with a long random secret Hard-coded "secret", truncated keys
Multi-service / key distribution RS256 / PS256 / ES256 + JWKS Shipping private keys to browsers
Explicitly disabled — none, algorithm confusion, accepting alg from attackers

Rules that hold up in production:

  1. Pin allowed algorithms in code — never “whatever the header says”
  2. Separate verify keys from sign keys for asymmetric setups
  3. Reject alg: none and unexpected alg values before crypto
  4. Prefer asymmetric when more than one service must verify

Library defaults are not a threat model. Your allowlist is.

Claims: small, intentional, verified

An ideal access-token payload is dull:

{
  "iss": "https://auth.example.com/",
  "aud": "https://api.example.com/",
  "sub": "1234567890",
  "iat": 1792155474,
  "nbf": 1792155474,
  "exp": 1792156374,
  "jti": "1c2a9f0e6b7d4a11"
}

Practices that matter:

  • Always validate exp (and usually nbf)
  • Validate iss and aud against an allowlist — strings must match your expected values
  • Use jti when you need replay tracking or denylist entries
  • Keep PII out of the payload when a random sub will do
  • Do not put secrets, passwords, or raw permissions dumps in claims you would regret logging

JWTs are signed (or encrypted, if you use JWE). Signed ≠ confidential. Anyone holding the token can read the payload unless you encrypt.

Lifetimes, refresh, and rotation

Secure defaults look like this:

  • Access token: minutes, not days (often 5–15 minutes for browser apps)
  • Refresh token: longer, opaque, server-tracked, rotated on use
  • Signing keys: rotatable via JWKS (kid), with overlap during rollout
  • Clock skew: small leeway (seconds), not “ignore exp in staging forever”

If refresh tokens live in a SPA, treat theft as account takeover: bind them, rotate them, detect reuse, and revoke families on anomaly.

Storage and transport

  • Send tokens only over HTTPS
  • Prefer HttpOnly + Secure + SameSite cookies for browser session tokens when XSS is in your threat model
  • If you must use headers (Authorization: Bearer), assume any XSS can exfiltrate them
  • Never put long-lived JWTs in localStorage “for convenience”
  • Do not log full tokens in app or proxy logs

Verification checklist (ideal path)

On every request that trusts a JWT:

  1. Parse compact serialization; require three segments (for JWS)
  2. Read kid / header metadata without trusting alg blindly
  3. Fetch/select key from your allowlist or JWKS
  4. Verify signature with pinned algorithm
  5. Validate iss, aud, exp, nbf
  6. Apply authorization using server-side roles/policies — claims are hints, not a substitute for policy engines when stakes are high

Debug and review with JWT Debugger

Before you argue about a token in Slack, decode it, verify the signature path, and run a security review for weak algs, bad lifetimes, and claim gaps. Use JWT Debugger on this site — encode/decode, HS/RS/PS/ES/EdDSA options, claims view, and misconfig Issue/Fix notes — to inspect alg, claims, and signature posture before the token hits production traffic.

Paste a staging token into the debugger and ask: would I accept this exp, this aud, and this algorithm in production tonight?

A minimal “ideal” profile

If you need a one-screen target state:

  • Asymmetric signing for multi-service verify (RS256 or ES256)
  • JWKS published for verifiers; private key only on the issuer
  • Access JWT ≤ 15 minutes; opaque refresh with rotation
  • Mandatory iss + aud + exp checks
  • Algorithm allowlist enforced in one shared verify helper
  • No none, no shared “dev secret” in prod, no forever tokens

JWTs fail closed when teams treat them as magic auth. They hold up when teams treat them as signed bearer credentials with a short fuse — verified every time, rotated often, and never trusted by shape alone.