A JSON Web Token (JWT) is a sophisticated yet elegant means of representing claims securely between two parties. Its structure is ingeniously segmented into three distinct parts: the Header, the Payload, and the Signature. Each of these components plays a crucial role in the token's functionality and security.
{ "alg": "HS256", "typ": "JWT" }
{ "sub": "1234567890", "name": "John Doe", "admin": true }
HMACSHA256( base64UrlEncode(header) + "." + base64UrlEncode(payload), secret)
The creation of a JWT involves encoding the header and payload and then signing these together. The cryptographic signing validates the authenticity and integrity of the token. If any part of the header or payload is altered, the verification process will fail, thus ensuring the token's security.
Code snippet: A simple JWT token example with a breakdown of its components:
Header: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9 Payload: eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9 Signature: TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ