如何使用JavaScript解码JWT的有效负载?没有图书馆。因此,令牌仅返回可由我的前端应用程序使用的有效负载对象。令牌示例: xxxxxxxxx.XXXXXXXX.xxxxxxxx结果是有效负载:{exp: 10012016 name: john doe, scope:['admin']}
3 回答

开满天机
TA贡献1786条经验 获得超13个赞
起作用的Unicode文本JWT解析器功能:
function parseJwt (token) {
var base64Url = token.split('.')[1];
var base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
var jsonPayload = decodeURIComponent(atob(base64).split('').map(function(c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
}).join(''));
return JSON.parse(jsonPayload);
};

牛魔王的故事
TA贡献1830条经验 获得超3个赞
您可以使用jwt-decode,因此可以编写:
import jwt_decode from 'jwt-decode';
var token = 'eyJ0eXAiO.../// jwt token';
var decoded = jwt_decode(token);
console.log(decoded);
/*{exp: 10012016 name: john doe, scope:['admin']}*/
添加回答
举报
0/150
提交
取消