A JWT (JSON Web Token) securely transfers information between two parties. It has three parts that keep data safe and verifiable.
JWT structure (header, payload, signature) in Spring Boot
JWT = header.payload.signature
header = {"alg": "HS256", "typ": "JWT"}
payload = {"sub": "1234567890", "name": "John Doe", "iat": 1516239022}
signature = HMACSHA256(base64UrlEncode(header) + "." + base64UrlEncode(payload), secret)The header and payload are JSON objects encoded in Base64Url.
The signature is created by hashing the header and payload with a secret key.
{"alg":"HS256","typ":"JWT"}{"sub":"1234567890","name":"John Doe","iat":1516239022}signature = HMACSHA256(base64UrlEncode(header) + "." + base64UrlEncode(payload), secret)This Spring Boot application exposes a REST endpoint GET /jwt/structure that generates a JWT token and returns its three parts as JSON. Add spring-boot-starter-web to your pom.xml, run with mvn spring-boot:run, and access http://localhost:8080/jwt/structure.
package com.example.jwtstructure; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import java.util.Base64; import java.util.LinkedHashMap; import java.util.Map; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; @SpringBootApplication @RestController public class JwtStructure { public static void main(String[] args) { SpringApplication.run(JwtStructure.class, args); } @GetMapping("/jwt/structure") public Map<String, String> jwtStructure() throws Exception { String headerJson = "{\"alg\":\"HS256\",\"typ\":\"JWT\"}"; String payloadJson = "{\"sub\":\"1234567890\",\"name\":\"John Doe\",\"iat\":1516239022}"; String secret = "secret"; String headerEncoded = Base64.getUrlEncoder().withoutPadding().encodeToString(headerJson.getBytes(java.nio.charset.StandardCharsets.UTF_8)); String payloadEncoded = Base64.getUrlEncoder().withoutPadding().encodeToString(payloadJson.getBytes(java.nio.charset.StandardCharsets.UTF_8)); String data = headerEncoded + "." + payloadEncoded; Mac hmac = Mac.getInstance("HmacSHA256"); SecretKeySpec keySpec = new SecretKeySpec(secret.getBytes(java.nio.charset.StandardCharsets.UTF_8), "HmacSHA256"); hmac.init(keySpec); byte[] signatureBytes = hmac.doFinal(data.getBytes(java.nio.charset.StandardCharsets.UTF_8)); String signatureEncoded = Base64.getUrlEncoder().withoutPadding().encodeToString(signatureBytes); String jwt = data + "." + signatureEncoded; Map<String, String> response = new LinkedHashMap<>(); response.put("Header (Base64Url)", headerEncoded); response.put("Payload (Base64Url)", payloadEncoded); response.put("Signature (Base64Url)", signatureEncoded); response.put("Complete JWT Token", jwt); return response; } }
The signature part protects the token from being changed by others.
Base64Url encoding is like Base64 but safe for URLs (no + or / characters).
Never share your secret key publicly; it keeps your tokens secure.
For Spring Boot, include <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency> in pom.xml.
A JWT has three parts: header, payload, and signature.
The header and payload are JSON data encoded in Base64Url.
The signature is a hash that proves the token is authentic and unchanged.
Practice
Solution
Step 1: Understand JWT parts
A JWT has three parts: header, payload, and signature.Step 2: Identify algorithm info location
The header contains metadata including the signing algorithm used.Final Answer:
Header -> Option BQuick Check:
Algorithm info = Header [OK]
- Confusing payload with header
- Thinking signature contains algorithm info
- Assuming issuer is a JWT part
Solution
Step 1: Recall JWT format
A JWT is a string with three parts separated by dots.Step 2: Confirm correct order
The order is header first, then payload, then signature.Final Answer:
Header.Payload.Signature -> Option AQuick Check:
JWT order = Header.Payload.Signature [OK]
- Mixing up header and payload order
- Placing signature in the middle
- Assuming signature comes first
eyJhbGciOiJIUzI1NiJ9.eyJ1c2VyIjoiam9obiJ9.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c, what does the middle part represent?Solution
Step 1: Identify JWT parts by position
The JWT has three parts separated by dots: header.payload.signature.Step 2: Locate the middle part
The middle part is the payload, which contains user data encoded in Base64Url.Final Answer:
Encoded payload -> Option DQuick Check:
Middle JWT part = Payload [OK]
- Confusing payload with header
- Thinking signature is in the middle
- Assuming algorithm is separate part
Solution
Step 1: Understand the role of signature
The signature proves the token is authentic and unchanged.Step 2: Consequence of missing signature
Without the signature, the token cannot be verified and may be tampered with.Final Answer:
The token cannot be verified for authenticity -> Option CQuick Check:
Missing signature = No verification [OK]
- Thinking payload becomes unreadable
- Assuming header JSON breaks
- Believing token expires immediately
Solution
Step 1: Decode header and payload
First, decode the header and payload from Base64Url to read their contents.Step 2: Verify signature using secret key
Use the secret key and header info to verify the signature matches the token data.Final Answer:
Decode header and payload, then verify signature using secret key -> Option AQuick Check:
Decode then verify signature = Correct process [OK]
- Trying to verify signature before decoding
- Ignoring signature verification
- Decoding signature as if it contains data
Ready to prove it?
Pass the test and this pattern moves unknown
