What is a JSON Web Token (JWT)?
A comprehensive guide to understanding JWTs, their structure, security best practices, and why they dominate modern web authentication.
The Basics of JWT
JSON Web Token (commonly referred to as JWT and pronounced "jot") is an open, industry-standard (RFC 7519) method for representing claims securely between two parties. Unlike older authentication systems that rely heavily on server-side session cookies and database queries, JWTs are designed to be self-contained. This means the token itself carries all the information necessary for the server to verify the user's identity without performing an additional database lookup.
Because JWTs are fundamentally just JSON objects that have been digitally signed (and optionally encrypted), they have become the absolute standard for securing RESTful APIs, Single Page Applications (SPAs), and Microservices.
How is a JWT Structured?
If you look at raw JWT, it usually looks like a long, scrambled string of characters separated into three distinct segments by dots (.).
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c
- 1. Header (Red)
The header typically consists of two parts: the type of the token (which is JWT), and the signing algorithm being used, such as HMAC SHA256 (HS256) or RSA. It is then Base64Url encoded to form the first part of the token. - 2. Payload (Green)
The payload contains the "claims". Claims are statements about an entity (typically, the user) and additional metadata. There are three types of claims: registered (predefined likeiss,exp), public, and private claims. Like the header, the payload is Base64Url encoded to form the second part of the token. Keep in mind that unless you are using JWE (JSON Web Encryption), the payload is easily readable by anyone, so do not put passwords or secret keys here! - 3. Signature (Blue)
To create the signature, the algorithm takes the encoded header, the encoded payload, a secret key known only to the server, and mathematically signs them. This signature ensures that the token wasn't altered in transit. If someone modifies the payload of the token, the signature validation process on the server will fail immediately.
Why Use JWTs? Performance and Statelessness
Before JWTs, developers typically saved a "Session ID" in a database and sent that ID to a user via a cookie. When the user made a request, the server had to hit the database to retrieve the session details and confirm the user's identity.
JWTs solve this bottleneck by being stateless. Since the server computationally verifies the cryptographic signature on the token using its secret key, there is absolutely no need to query a database to see if the session is valid. This makes JWTs incredibly fast and infinitely scalable across distributed networks and microservices.
Security Best Practices
- Never store sensitive data in the Payload: Base64 is not encryption. Anyone can decode a JWT payload. Only store safe things like User IDs.
- Keep token lifespans short: Because JWTs are stateless, they are hard to revoke before they expire. Use short expiration times (
expclaim) like 15 minutes, and issue a Refresh Token to get a new one. - Always validate the Signature: A JWT is meaningless unless the server verifies the signature mathematically matches the header and payload.
Try it yourself!
Want to see exactly what is inside a JSON Web Token? Paste any token into our secure, client-side decoder to break apart the Header and Payload instantly.
Open JWT Decoder Tool