JWT Structure: Header, Payload, and Signature
You'll learn: How a JWT is physically constructed from three distinct encoded parts and what each section contains.
The Three-Part Structure
A JWT looks like this: xxxxx.yyyyy.zzzzz — three Base64-encoded strings separated by dots. Think of it like a sandwich with three distinct layers, each serving a specific purpose.
Header
The header (first part) specifies metadata about the token itself:
typ: Token type (usually"JWT")alg: Algorithm used for signing (like"HS256"or"RS256")
Decoded example:
{
"alg": "HS256",
"typ": "JWT"
}
Payload
The payload (second part) contains the actual claims — statements about the user or session. This is where you put data like:
sub: Subject (user ID)iat: Issued at timestampexp: Expiration timestamp- Custom claims:
userId,role,email, etc.
Decoded example:
{
"sub": "1234567890",
"name": "John Doe",
"role": "admin",
"iat": 1516239022,
"exp": 1516242622
}
Signature
The signature (third part) is the security seal. It's created by:
- Taking the encoded header + encoded payload
- Signing them with a secret key (or private key) using the algorithm specified in the header
This proves the token hasn't been tampered with. If someone changes the payload, the signature won't match when verified.
Why Three Parts?
The header tells you how to verify. The payload tells you what the token claims. The signature lets you prove those claims are legitimate.
Key Takeaway: A JWT consists of three Base64-encoded parts separated by dots: the header (metadata), payload (claims/data), and signature (cryptographic proof of authenticity). Understanding this structure is essential for working with JWTs securely.