JSON Web Tokens Explained: JWTs vs Sessions Compared

JSON Web Tokens explained in plain terms starts with a boarding pass. A JSON Web Token, or JWT, is a small signed document a server hands you after you log in, which you then show at every door instead of proving who you are from scratch. The central question is why a token you can read yourself is safe to carry around, and the answer is the signature: anyone can read the claims inside, but only the server that holds the secret can create a valid one.

A JWT has three parts, separated by dots: a header naming the signing method, a payload of claims such as your user id and an expiry time, and a signature made over the first two parts. The payload is only encoded, not encrypted, so its contents are visible to anyone holding the token, and it must never contain a password or anything else sensitive.

Because the token is self-contained, any service that knows the signing key can verify it without calling the server that issued it. This article explains how JWTs work, what belongs inside them, and how they compare with the older server-side session.

Key takeaways

  • A JWT is a signed, self-contained token with a header, a payload, and a signature.
  • The payload is readable by anyone, so it must never hold secrets or passwords.
  • Verification needs only the signing key, with no call back to the issuing server.
  • JWTs are easy to scale but hard to revoke before they expire.
  • Sessions suit browser apps with instant logout; JWTs suit APIs and services.

What a JWT is

A JWT is defined by RFC 7519, and its design goal is to move small claims between parties that trust each other, in a format that cannot be altered unnoticed. The header says how the token was signed, typically with HMAC using a shared secret or RSA using a private key, and the payload carries registered claims such as the issuer, the subject, the audience, the issued-at time, and the expiry time.

Developers also add custom claims, such as a user id or a role, but every claim should be treated as public information. The standard encoding is base64url, which keeps the token safe to put in a header or a URL, and the signature is computed over the header and payload together, so changing even one character breaks it.

The trust model is simple: whoever holds the signing key is trusted, and everyone else verifies. With a shared secret, any service holding it can both create and check tokens, while with an asymmetric key pair, only the issuer can sign, and any service with the public key can verify. That second arrangement is the one that scales across organisations.

How a JWT is used in practice

The usual flow begins at login. You send your credentials to the authentication server, it checks them, and it returns a JWT, often alongside a refresh token. Your app then attaches the JWT to every API request, typically in an authorisation header, and each service checks the signature and expiry before answering.

No service needs to remember you: a request can land on any server in a fleet, and the token carries everything needed to decide whether it is allowed. That is why JWTs became the default for microservices and mobile apps.

The cost of that convenience is revocation. A server-side session can be deleted the moment you log out or change your password, but a JWT stays valid until its expiry time, because nobody keeps a list of live tokens. The standard answer is to keep access tokens very short-lived, often minutes, and to use a refresh token, stored safely, to mint new ones.

Storage matters as much as design. A JWT in local storage can be stolen by any script that slips onto the page, while one in a cookie with the right flags is safer but needs protection against cross-site request forgery. No storage choice is safe on its own.

Close-up photograph of a keycard and a blank sheet of paper lying on a wooden desk beside a laptop keyboard
Close-up photograph of a keycard and a blank sheet of paper lying on a wooden desk beside a laptop keyboard, showing the idea behind a JWT: a signed pass you carry and present.

JWTs vs server-side sessions

Sessions are the older answer to the same problem. The server creates a random session id, stores your state beside it, and hands your browser a cookie holding only that id. Every request is checked against the store, which makes logout instant and revocation trivial, but it also means every request needs a lookup, and the store must be shared across servers.

JWTs trade that lookup for self-containment. Verification is a local calculation, so it is fast and works across services without shared state, but the token cannot be taken back early, and it grows with every claim you add, travelling on every request. The two approaches are not rivals so much as tools for different shapes of problem.

The table below sets out the main contrasts.

Aspect JWT Server-side session
Where state lives Inside the token itself In a store on the server
Verification Local signature check Lookup by session id
Revocation Hard before expiry Instant, delete the session
Scales across services Yes, no shared store needed Needs a shared or sticky store
Best for APIs and microservices Browser apps with logout

Most teams land on sessions for browser applications, where instant logout matters, and JWTs for APIs, where independent verification matters more. Hybrids are common too: a short-lived JWT backed by a revocable refresh token gives most of the benefits of both.

What belongs in a JWT, and what does not

A good payload is small and boring: a user id, an expiry time, the issuer, and the audience. A role claim is fine when roles change rarely, but anything that changes often, such as permissions or a subscription status, should be fetched fresh instead of frozen into a token that lives for an hour.

A bad payload is a data leak waiting to happen. Passwords, email addresses, addresses, and internal identifiers have all been found inside JWTs by attackers who simply decoded them, because decoding a JWT takes seconds and needs no key at all. If a claim would embarrass you on a public billboard, it does not belong in a token.

Size is a second constraint. A token travels on every request, so a payload stuffed with preferences slows everything down and can trip header size limits. Keep it small and fetch the rest when needed.

Finally, always check the expiry, issuer, and audience, and the signature with the right algorithm. The most famous JWT failures came from servers that accepted tokens signed with no algorithm at all, or trusted the algorithm named in the token header.

Where JWTs are used

Single sign-on systems use JWTs as identity tokens. Our guide to SAML explains the older enterprise standard for the same job, and OpenID Connect, which layers identity onto OAuth 2.0, issues a JWT called an ID token that carries the verified facts about who signed in.

APIs use JWTs to authorise calls without a central session store. A mobile app logs in once, receives a token, and presents it with every request, and the API gateway verifies the signature at the edge before the request reaches any business logic.

Microservices use JWTs to pass user context between services. The first service checks the login, and the token then travels downstream, so each service can see who the request is for without asking the authentication service again. Our guide to passkeys explains a newer login method that often issues JWTs once the user has proved who they are.

Even stateless download links use the same idea. A signed URL is, in effect, a JWT for a single file, granting access until its expiry and no longer, which is how cloud storage hands out temporary access without creating accounts. Our guide to TLS explains how these tokens travel inside encrypted connections.

Common mistakes and how to avoid them

The most damaging mistake is accepting the none algorithm, or letting the token header choose the algorithm. An attacker rewrites the header, drops the signature, and the server, if it trusts the header, lets them in. The fix is simple: the server must decide the algorithm, and reject anything else.

The second mistake is trusting a token without checking its expiry, issuer, or audience. A token minted for one service should never be accepted by another, and a token from last month should never open a door today. Verify all three on every request, not just the signature.

The third is putting sensitive data in the payload. Assume every token will be read by its holder, because it will be, and keep the payload to identifiers and expiry. Anything else belongs in a database, fetched with the token as the key.

The fourth is long-lived access tokens with no revocation plan. A token that lives for a week is a stolen credential that works for a week. Keep access tokens short, rotate refresh tokens, and keep a blocklist for the rare cases where you must kill a token early.

JSON Web Tokens checklist

  • Keep the payload small and free of anything sensitive.
  • Sign with a strong algorithm chosen by the server, never by the token.
  • Reject tokens with the none algorithm outright.
  • Check signature, expiry, issuer, and audience on every request.
  • Keep access tokens short-lived and rotate refresh tokens.
  • Store tokens in the browser with care, and prefer secure cookies.
  • Use asymmetric signing when many services must verify tokens.
  • Never trust claims that change often; fetch fresh permissions instead.
  • Plan revocation before you need it, even if it is only a short blocklist.

Frequently asked questions

Is a JWT encrypted?

No, a standard JWT is only encoded and signed. Anyone holding it can read the payload, which is why it must never contain passwords, personal details, or anything you would not show its owner.

How is a JWT different from a session cookie?

A session cookie holds only a random id, with the real state on the server, while a JWT carries its claims inside itself. Sessions revoke instantly but need a shared store; JWTs verify anywhere but cannot be revoked before expiry.

Can a stolen JWT be used by someone else?

Yes, until it expires, which is why access tokens should live for minutes, not days. Short lifetimes, secure storage, and refresh-token rotation are the defences that limit the damage.

What does the signature actually prove?

It proves the token was created by someone holding the signing key and has not been changed since. It does not prove the claims are true, only that the issuer asserted them.

Should I store a JWT in local storage?

It is the riskiest option, because any script on the page can read it. A cookie with the secure and HTTP-only flags is safer for browser apps, provided the app also defends against cross-site request forgery.

When should I choose sessions instead?

When instant logout and revocation matter most, as in banking or admin panels, sessions are the better choice. JWTs win when many independent services must verify requests without a shared store.

The bottom line

JSON Web Tokens explained in one line is this: a signed, self-contained pass that any service can verify, carrying only what its holder is allowed to read.

Used with short lifetimes, minimal claims, and a fixed signing algorithm, they are the simplest way to authorise APIs at scale. Used with secrets inside and week-long expiry, they are a credential leak with a signature on it.

This article is general information about how JSON Web Tokens work, not advice about securing a particular application.

Latest articles

Scroll to Top