JWT Explained: Structure, Claims, Signing and Common Pitfalls
7 min read · Published 1 November 2026 · Updated 2 November 2026
A JSON Web Token (JWT) is a compact string that carries a few facts about a user or a session and proves that nobody changed them. It is used for login sessions, API access and service-to-service calls. Because a JWT looks like random characters, it is easy to misunderstand. This guide explains what is inside one, what the signature does and does not protect, and the mistakes that cause real security problems.
In this guide
The three parts
A JWT is three pieces separated by dots: header.payload.signature. The header and payload are JSON documents encoded with Base64URL, a variant of Base64 that uses - and _ instead of + and / and drops the = padding so the token is safe in URLs. Encoding is not encryption: anyone can decode the first two parts and read them.
Decoded example
Header: { "alg": "HS256", "typ": "JWT" }
Payload: { "sub": "1234567890", "name": "Lan",
"iat": 1700000000, "exp": 1700003600 }
Signature: HMAC-SHA256(
base64url(header) + "." + base64url(payload),
secret
)The header
The header says how the token was signed. The alg field names the algorithm, for example HS256 (HMAC with SHA-256, using one shared secret) or RS256 (RSA signature, using a private key to sign and a public key to verify). The typ field is usually "JWT".
The payload and its claims
The payload holds claims, which are statements about the subject. A few are registered by the standard and widely understood:
- iss (issuer): who created the token.
- sub (subject): who the token is about, usually a user ID.
- aud (audience): who the token is meant for.
- exp (expiration time): the moment after which the token must be rejected.
- nbf (not before): the moment before which the token must be rejected.
- iat (issued at): when the token was created.
- jti (JWT ID): a unique identifier, useful for revocation lists.
Dates are Unix timestamps
The exp, nbf and iat claims are numbers of seconds since 1 January 1970 UTC, not milliseconds. A common bug is generating them in milliseconds, which creates a token that appears to expire thousands of years in the future. When a token is rejected as expired, convert exp to a readable date and compare it with the server clock.
What the signature does
The signature is computed over the encoded header and payload. If anyone changes a single character of either part, the signature no longer matches, so the receiver can detect tampering. With HS256, both the issuer and the verifier know the same secret. The secret must be long and random: the standard requires a key at least as long as the hash output, which is 256 bits (32 random bytes) for HS256.
Decoding is not verifying
Reading the payload tells you nothing about whether the token is genuine. Your server must verify the signature with the expected key and algorithm, and then check exp, nbf, iss and aud, before trusting any claim. Decoding tools are for debugging. Production code must always verify.
Common pitfalls
- Putting secrets in the payload. A standard signed JWT is readable by anyone who holds it. Do not store passwords, card numbers or other sensitive data in it. If a payload must be confidential, use an encrypted token format (JWE) instead.
- Accepting the algorithm named in the token. Fix the algorithm on the server. Historically, libraries that trusted the alg header allowed "none" (no signature) or let attackers switch from RS256 to HS256 and sign with the public key.
- Weak or guessable HS256 secrets. A short secret can be brute-forced offline from a single captured token.
- Very long lifetimes. A JWT cannot be recalled before it expires unless you keep a deny list, so keep access tokens short, for example minutes, and renew them with a separate refresh token.
- Careless storage. Tokens stored where page scripts can read them are exposed if the site has a cross-site scripting bug. Prefer HttpOnly cookies where your architecture allows it.
- Skipping audience and issuer checks. A token that is valid for one service should not be accepted by another.
When a JWT is a good fit
JWTs work well for stateless APIs and for passing short-lived, signed identity information between services. For a classic website with server-side sessions, an opaque session ID stored on the server is often simpler and easier to revoke. Choose the tool that matches how much state you want the server to keep.
A typical login flow with access and refresh tokens
- The user signs in with a username and password (or another method).
- The server checks the credentials and returns a short-lived access token (a JWT, valid for minutes) and a longer-lived refresh token.
- The client sends the access token with each API request, usually in the Authorization header.
- When the access token expires, the client sends the refresh token to get a new access token without asking the user to sign in again.
- On sign-out, or if a token is suspected to be stolen, the server revokes the refresh token so no new access tokens can be issued.
Checklist for verifying a token on the server
- Split the token into three parts and reject anything that does not have exactly three.
- Check that the algorithm in the header is one you allow, and verify the signature with the matching key. Never accept "none".
- Check exp (not expired) and nbf (already valid), allowing a small clock tolerance of a few seconds.
- Check iss (expected issuer) and aud (meant for this service).
- Only then read the claims such as sub and any roles, and still check permissions on the server for the action requested.
Frequently asked questions
- JWT or a session cookie: which should I use?
- A server-side session with an opaque ID is simple and easy to revoke, which suits a classic website. JWTs fit stateless APIs and services that need to pass signed identity information without a shared session store. Many systems use both.
- Can I revoke a JWT before it expires?
- Not by itself, since the signature stays valid until exp. You can keep the lifetime short, maintain a deny list of token IDs (jti), or rely on revoking the refresh token so that no new access tokens are issued.
- HS256 or RS256?
- HS256 uses one shared secret, so every service that verifies tokens can also create them. RS256 uses a private key to sign and a public key to verify, which lets many services verify tokens without being able to forge them.
- Where should a browser store a JWT?
- Any storage that page scripts can read is exposed if the site has a cross-site scripting bug. An HttpOnly, Secure cookie with appropriate SameSite settings is usually safer, though it needs protection against cross-site request forgery.
- What is the "kid" header?
- It is a key ID. When a service rotates signing keys, kid tells the verifier which key was used so it can pick the right one.
Sources and further reading
Try it yourself: JWT Encoder & Decoder runs in your browser, free and with no sign-up.