Updated: 2018-09-26 02:21:10 CST +08
JWT - JSON Web Token
Purpose
Store data in client side, which could be read by can’t be modified.
- Authentication
- Authorization
Structure
base64(header).base64(payload).signature
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJEaW5vIExhaSIsInN1YiI6ImRpbm9zODAxNTJAZ21haWwuY29tIiwiYXVkIjoiZGlub2xhaS5jb20iLCJleHAiOjE1MzczNTcyNjIsImlhdCI6MTUzNzM1NzE2MiwidXNlcklkIjo4MDE1Mn0.YaLyoBs8z5Va7YsIQaC6uEZDw8GZHBiV_2hIUSVQYUs
{
"alg": "HS256",
"typ": "JWT"
}
Payload
{
"iss": "Dino Lai",
"sub": "dinos80152@gmail.com",
"aud": "dinolai.com",
"exp": 1537357262,
"iat": 1537357162,
"userId": 80152
}
Signature
Encrypt by algorithm defined in header
HmacSHA256(base64(header)+"."+base64(payload), $secret)
Flow
Comparison
Comparison |
JWT |
Cookie |
Session |
Side |
Client |
Client |
Server |
Visible |
✓ |
✓ |
☓ |
Tamper |
☓ |
✓ |
☓ |
Identify |
✓ |
☓ |
✓ |
additional resource |
spend computing for en/decode, encrypt |
|
diskIO or network IO |
Reference