乐趣区

关于javascript:Day-77100-前端如何解析Token为可读的内容

(一)应用 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…

退出移动版