jwt生成token和token解析基礎(chǔ)詳解
1.jwt結(jié)構(gòu)
jwt生成到客戶端(瀏覽器)的token包含"."分開的三個部分:
- header(Base64Url編碼過的)
- payload(Base64Url編碼過的)
- signature
形如:xxxxx.yyyyy.zzzzz
1.1 例子:
eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJuYW1lIjoiYW5keSIsImV4cCI6MTY1NTg5NzEwMCwiYWdlIjozMH0.32hfc-oBxGg2Lgk3QR48HCbadsbOfCUxexw9aiQ_FQk
拆為3部分:
eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.(header)
eyJuYW1lIjoiYW5keSIsImV4cCI6MTY1NTg5NzEwMCwiYWdlIjozMH0.(payload)
32hfc-oBxGg2Lgk3QR48HCbadsbOfCUxexw9aiQ_FQk(signature)
2.header+payload+signature介紹
2.1 header
上面的header部分:eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9
base64Url解碼后:
{ "typ": "JWT", "alg": "HS256" }
通常說明token的類型、生成token所使用的的算法
The header typically consists of two parts: the type of the token, which is JWT, and the signing algorithm being used, such as HMAC SHA256 or RSA.
2.2 Payload
上面的Payload部分:eyJuYW1lIjoiYW5keSIsImV4cCI6MTY1NTg5NzEwMCwiYWdlIjozMH0
base64Url解碼后:
{ "name": "andy", "exp": 1655897100, "age": 30 }
通常是要客戶端請求時帶貨的內(nèi)容(比如用戶名,比如是否是管理員等,server端生成的時候可以定義內(nèi)容,形式如map)
The second part of the token is the payload, which contains the claims. Claims are statements about an entity (typically, the user) and additional data. There are three types of claims: registered, public, and private claims.
2.3 Signature
上面的Signature部分:32hfc-oBxGg2Lgk3QR48HCbadsbOfCUxexw9aiQ_FQk
它是用來驗簽的, 驗證是否被客戶端修改過,它的生成邏輯如下:
就是使用header部分的base64Url、payload部分的base64Url部分、小圓點、以及你的私鑰密碼,使用指定的算法生成的;因為有密碼, 所以是安全的,這也是密碼要保護好的原因。
計算邏輯如下:
HMACSHA256(
base64UrlEncode(header) + "." + base64UrlEncode(payload),
12345
)
3. java測試用例
/** * JWT加密生成token, payload中保存 name/age */ @Test public void testJwtToken() { // 加密秘鑰 final String SECRET = "12345"; Calendar c = Calendar.getInstance(); c.add(Calendar.HOUR, 2); String token = JWT.create().withClaim("name", "andy") .withClaim("age", 30) .withExpiresAt(c.getTime()) .sign(Algorithm.HMAC256(SECRET)); System.out.println(token); } /** * JWT解密生成token, 讀取payload中保存的 name/age */ @Test public void testJwtVerify() { String jwtToken = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJuYW1lIjoiYW5keSIsImV4cCI6MTY1NTg4ODk3MiwiYWdlIjozMH0.LU4AQJkld03kDhatkiiArSJI4liGiANArTvoyswzk5I"; final String SECRET = "12345"; JWTVerifier verifier = JWT.require(Algorithm.HMAC256(SECRET)).build(); DecodedJWT decodedJWT = verifier.verify(jwtToken); Claim name = decodedJWT.getClaim("name"); Claim age = decodedJWT.getClaim("age"); System.out.println(name); System.out.println(age); }
以上就是jwt生成token和token解析基礎(chǔ)的詳細內(nèi)容,更多關(guān)于jwt生成token的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
在SpringBoot中實現(xiàn)線程池并行處理任務(wù)的方法詳解
在使用Spring Boot開發(fā)應(yīng)用程序時,我們經(jīng)常需要處理一些耗時的任務(wù),例如網(wǎng)絡(luò)請求、數(shù)據(jù)庫操作或者其他需要花費一定時間的計算任務(wù),本文將介紹如何在Spring Boot中使用線程池來實現(xiàn)任務(wù)的并行處理2023-06-06java定時任務(wù)cron表達式每周執(zhí)行一次的坑及解決
這篇文章主要介紹了java定時任務(wù)cron表達式每周執(zhí)行一次的坑及解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06詳解JAVA中的Collection接口和其主要實現(xiàn)的類
這篇文章主要介紹了JAVA中的Collection接口和其主要實現(xiàn)的類,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03