JSON Web Token (JWT) is a compact, self contained and secure way to exchange information between two parties using a JSON object.
Imagine you're visiting an all-inclusive resort. When you check in, you get a special wristband. This wristband proves to the staff that you're a guest and grants you access to everything—the pool, the buffet, the evening shows. You don't need to show your ID every time; the wristband is enough.
A JWT is like that wristband. It's a compact, self-contained token that allows two parties to securely exchange information. Once a user logs in, the server creates a JWT and sends it to the client. The client then includes this token with every subsequent request to prove its identity.
JWT consists of three parts, separated by dots (.):
So, a JWT looks like this: hhh.ppp.sss where hhh is the header, ppp is the payload, and sss is the signature.
The header typically consists of two parts: the token type, which is JWT, and the signing algorithm being used, such as HMAC SHA256 or RSA.
Example Header:
{ "alg": "HS256", "typ": "JWT" }
This JSON is then Base64Url encoded to form the first part of the JWT.
The payload contains the "claims," which are statements about an entity (typically, the user) and additional data. There are three types of claims:
iss (issuer), exp (expiration time), and sub (subject).Example Payload:
{ "sub": "1234567890", "name": "John Doe", "admin": true, "iat": 1516239022 }
The payload is also Base64Url encoded to form the second part of the JWT.
To create the signature, you take the encoded header, the encoded payload, a secret key, and sign it with the algorithm specified in the header.
Example Signature:
HMACSHA256( base64UrlEncode(header) + "." + base64UrlEncode(payload), your-256-bit-secret )
The signature is crucial for security. It verifies that the sender of the JWT is who it says it is and ensures that the message wasn't changed along the way. If some one tampers with the token, the signature will be invalid and the server will reject the request. JWTs can be signed using a secret (with HMAC algorithm) or a public/private key pair using RSA or ECDSA.
Here's a typical authentication flow:
HttpOnly cookie or localStorage).Authorization header, typically using the Bearer schema: Authorization: Bearer <token>.401 Unauthorized error.In modern authentication systems, you'll often encounter two main types of tokens working together:
Want to see a full implementation in Java with Spring Security?
Check out JWT Authentication with Spring 6 Security: Complete Guide for hands-on code and step-by-step building of secure authentication using JWTs in a real-world Spring Boot application.
Complete guide to JWT authentication with Spring 6 Security. Learn best practices, implementation, and security without custom filters.
A comprehensive guide to understand HTTP cookies and how to manage them on both the client and server.

Get instant AI-powered summaries of YouTube videos and websites. Save time while enhancing your learning experience.