How to jwt tokens work
Content on WhatAnswers is provided "as is" for informational purposes. While we strive for accuracy, we make no guarantees. Content is AI-assisted and should not be used as professional advice.
Last updated: April 4, 2026
Key Facts
- JWTs are stateless, meaning the server doesn't need to store session information.
- A JWT is composed of three parts: Header, Payload, and Signature, separated by dots.
- The Header typically contains metadata about the token, like the signing algorithm.
- The Payload contains claims, which are statements about an entity (usually the user) and additional data.
- The Signature is used to verify that the sender of the JWT is who it says it is and to ensure that the message wasn't changed along the way.
Overview
JSON Web Tokens (JWTs) are a popular standard (RFC 7519) for creating access tokens that assert some number of claims. They are a compact, URL-safe means of representing claims to be transferred between two parties. JWTs are commonly used in web applications for authentication and authorization purposes, allowing a server to verify the identity of a user without needing to maintain session state on the server itself. This makes them a crucial component in modern, distributed, and stateless application architectures.
What is a JWT?
A JWT is essentially a string that is composed of three parts, separated by dots (.). These parts are:
- Header: The header is a JSON object that typically contains information about the token, such as the type of token (JWT) and the signing algorithm being used (e.g., HMAC SHA256 or RSA). This JSON object is Base64Url encoded.
- Payload: The payload is a JSON object that contains the claims. Claims are statements about an entity (typically, the user) and any additional data. There are three types of claims: registered claims, public claims, and private claims. Registered claims are pre-defined and recommended but not mandatory. Public claims are defined by those using JWTs but should be registered in the IANA JSON Web Token Registry or be a URI that contains a collision-resistant identifier. Private claims are custom claims created to share information between parties that agree on their structure. The payload is also Base64Url encoded.
- Signature: The signature is created by taking the encoded header, the encoded payload, a secret (for symmetric algorithms) or a private key (for asymmetric algorithms), and signing them using the algorithm specified in the header. The signature is used to verify that the sender of the JWT is who it says it is and to ensure that the message wasn't changed along the way.
How JWTs Work in Authentication
The typical flow for using JWTs for authentication is as follows:
- User Login: A user logs in to a web application by providing their credentials (e.g., username and password).
- Server Verification: The server verifies the user's credentials.
- Token Generation: If the credentials are valid, the server generates a JWT. This token contains information about the user (claims) and is signed by the server using a secret key or a private key.
- Token Transmission: The server sends the JWT back to the client (e.g., the user's browser).
- Client Storage: The client stores the JWT, often in local storage or a cookie.
- Subsequent Requests: For subsequent requests to protected resources, the client includes the JWT in the HTTP request's Authorization header, typically in the format 'Bearer [token]'.
- Server Verification: The server receives the request, extracts the JWT, and verifies its signature using the same secret key or public key used during generation. If the signature is valid, it means the token hasn't been tampered with and was issued by the server. The server can then trust the claims within the payload and grant access to the requested resource without needing to query a database for user session information.
Benefits of Using JWTs
- Statelessness: JWTs allow applications to be stateless. The server doesn't need to store any session data, as all the necessary information is contained within the token itself. This simplifies scaling and improves performance.
- Compactness: JWTs are compact and can be easily transmitted in URLs, POST parameters, or HTTP headers.
- Security: When signed correctly, JWTs can verify the integrity and authenticity of the claims. However, it's crucial to remember that the payload is only encoded, not encrypted, so sensitive information should not be placed there unless the token is encrypted.
- Interoperability: JWTs are a standard, making them interoperable across different programming languages and platforms.
When to Use JWTs
JWTs are well-suited for scenarios where:
- Authentication: Verifying user identity after login.
- Authorization: Granting specific permissions to users based on the claims in the token.
- Information Exchange: Securely exchanging information between parties.
- Microservices: Facilitating communication between different microservices.
Security Considerations
While JWTs offer security benefits, they are not a silver bullet. It's essential to be aware of potential vulnerabilities:
- Sensitive Data: Never store sensitive information (like passwords or credit card numbers) in the payload, as it is only Base64 encoded and can be easily decoded. For sensitive data, consider using JSON Web Encryption (JWE).
- Algorithm Choice: Use strong signing algorithms like RS256 (asymmetric) or HS256 (symmetric). Avoid 'none' as an algorithm.
- Token Expiration: Always include an expiration time (`exp` claim) in the payload and validate it on the server.
- Key Management: Securely store your secret keys or private keys. Compromised keys can lead to severe security breaches.
- Cross-Site Scripting (XSS): If storing JWTs in local storage, be mindful of XSS attacks that could steal tokens. Using HttpOnly cookies can mitigate this risk.
- Token Revocation: JWTs are stateless, which makes revoking them before expiration challenging. Solutions like maintaining a blacklist of revoked tokens on the server or using short expiration times with refresh tokens are common workarounds.
More How To in Daily Life
Also in Daily Life
More "How To" Questions
Trending on WhatAnswers
Browse by Topic
Browse by Question Type
Sources
- JWT.io - The Runtime for JSON Web Tokensfair-use
- RFC 7519 - JSON Web Token (JWT)CC-BY-4.0
- Using localStorage - Web APIs | MDNCC-BY-SA-2.5
Missing an answer?
Suggest a question and we'll generate an answer for it.