Sub topic 3
Anatomy of a JWT
This chapter assumes you know what a bearer token is and can read basic JSON. Later chapters cover the claims and the signature check in detail. Here you will learn how a JWT is put together.
The three parts
A JSON Web Token (JWT) is a string of three parts separated by dots:
header.payload.signatureEach of the first two parts is a JSON object, encoded with Base64url. The signature is raw bytes, also Base64url encoded.
| Part | Contains | Example |
|---|---|---|
| Header | Metadata about the token | `{"alg":"RS256","typ":"JWT","kid":"key-1"}` |
| Payload | Claims about the subject | `{"sub":"user-42","scope":"orders.read"}` |
| Signature | Proof that the issuer produced the first two parts | Binary data |
The header names the signing algorithm in alg. The kid field says which key was used. A resource server uses kid to pick the matching public key from the issuer's key set.
Base64url is encoding, not encryption
Base64url turns bytes into characters that are safe in URLs and HTTP headers. It uses - and _ instead of + and /, and it drops the = padding. Anyone holding the token can decode the header and payload.
The signature protects integrity. It does not hide anything. Do not put secrets such as passwords in a JWT payload.
Building and decoding a token
This program builds a small token signed with HMAC-SHA256, then splits it and decodes each part. It uses only the JDK. HS256 uses one shared secret and is used here only to keep the demo self-contained. Identity providers usually sign with an asymmetric algorithm such as RS256.
import static java.nio.charset.StandardCharsets.UTF_8;
import java.util.Base64;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
public class JwtAnatomy {
public static void main(String[] args) throws Exception {
Base64.Encoder enc = Base64.getUrlEncoder().withoutPadding();
String header = "{\"alg\":\"HS256\",\"typ\":\"JWT\"}";
String payload = "{\"iss\":\"https://issuer.example.com/realms/demo\","
+ "\"sub\":\"user-42\",\"scope\":\"orders.read\"}";
String signingInput = enc.encodeToString(header.getBytes(UTF_8))
+ "." + enc.encodeToString(payload.getBytes(UTF_8));
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(new SecretKeySpec(
"demo-secret-demo-secret-demo-secret!".getBytes(UTF_8), "HmacSHA256"));
String signature = enc.encodeToString(mac.doFinal(signingInput.getBytes(UTF_8)));
String token = signingInput + "." + signature;
System.out.println("Token: " + token);
String[] parts = token.split("\\.");
Base64.Decoder dec = Base64.getUrlDecoder();
System.out.println("Header: " + new String(dec.decode(parts[0]), UTF_8));
System.out.println("Payload: " + new String(dec.decode(parts[1]), UTF_8));
System.out.println("Signature bytes: " + dec.decode(parts[2]).length);
}
}The signature covers the exact text base64url(header) + "." + base64url(payload). If someone changes one character of the payload, the signature no longer matches, and validation fails.
What decoding does not tell you
Decoding a token does not make it trustworthy. Anyone can build a token with any payload. A resource server must verify the signature with a key it trusts before it reads a claim. Spring Security's JwtDecoder does this verification, and a later chapter configures it.
Common mistakes at this stage:
- Treating a decoded payload as proof of identity without checking the signature.
- Pasting production tokens into online decoders. The token is a live credential until it expires.
- Trusting the
algheader blindly. The server should accept only the algorithms it expects. Tokens withalg: nonemust be rejected. - Using the standard Base64 decoder on a JWT part. It fails on
-and_. Use the URL variant.
Exercise
A teammate says "the payload is Base64url encoded, so it is protected from clients." Is that correct? What does the signature protect instead?
Show answer
No. Base64url is a reversible encoding, so any holder can read the header and payload. The signature protects integrity. It lets the resource server detect that the header or payload was changed after the issuer signed them. It does not provide confidentiality.