JJWT in Spring Security
Using Java Json Web Token library for JWT in Spring Boot and Security
What is JWT?
JWT là một chuỗi 3 phần mang theo thông tin (claims).
Ví dụ:
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.
eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.
TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQJWT rất compact (gọn gàngàng, súc tích). Biểu diễn một chuỗi các thông tin (claims) and signature.
{
"alg": "HS256",
"typ": "JWT"
}
{
"sub": "1234567890",
"name": "John Doe",
"admin": true
}Các ứng dụng của JWT
Authentication
Authorization
Federated identity (Nhận dạng liên kết)
Client-side session (”stateless” session)
Client-side secrets
Challenges
Revoke token
Refresh token, rotate token
JJWT
Java Json Web Token Library (≥ v0.12.x)
Dependencies
Maven
Or Gradle
Code JJWT
1. Secret Key
Using raw-key utf8 in application.properties
2. Generate Token
From user details
With extra claims
Build token (create jwt)
Extract infomation from token
Include extract All and extract single claim.
Validate token
Validate token with general exception try-catch.
Authentication Entry Point
JwtAuthenticationFilter
Register Filter and EntryPoint to SecurityConfig
Q & N
I change final character of jwt and it still valid?
Because it is padding of base64. But your jwt is still original content after decode, each jwt is unique.
Last updated