Day 77/100 前端如何解析Token為可讀的內容?

SnowWolfArden發表於2022-11-24

(一)使用 JWT-Decode

jwt-decode是一個小的瀏覽器庫,用於幫助解碼Base64Url編碼的JWTs令牌。

周下載量 3,586,702

IMPORTANT: This library doesn't validate the token, any well formed JWT can be decoded. You should validate the token in your server-side logic by using something like express-jwt, koa-jwt, Owin Bearer JWT, etc.

只要,符合規範的JWT都能解碼

(二)使用DEMO

import jwt_decode from "jwt-decode";
 
var token = "eyJ0eXAiO.../// jwt token";
var decoded = jwt_decode(token);
 
console.log(decoded);
 
/* prints:
 * { foo: "bar",
 *   exp: 1393286893,
 *   iat: 1393268893  }
 */
 
// decode header by passing in options (useful for when you need `kid` to verify a JWT):
var decodedHeader = jwt_decode(token, { header: true });
console.log(decodedHeader);
 
/* prints:
 * { typ: "JWT",
 *   alg: "HS256" }
 */

參考連結

1、jwt-decode
https://www.npmjs.com/package...

2、token線上解壓地址
https://www.box3.cn/tools/jwt...

寫在最後的話

學習路上,常常會懈怠

《有想學技術需要監督的同學嘛~》
https://mp.weixin.qq.com/s/Fy...

相關文章