[英]@auth0/angular-jwt : Get claims from decoded token
?I get in my Angular 10 application JWT token by "@auth0/angular-jwt". ?我通过“@auth0/angular-jwt”进入我的 Angular 10 应用程序 JWT 令牌。 And after decoding function I get a list of claims like this:在解码 function 后,我得到了如下声明列表:
{
http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name: "johndoe",
http://schemas.microsoft.com/ws/2008/06/identity/claims/role: "Manager",
exp: 1525510870,
iss: "http://localhost:5000",
aud: "http://localhost:5000"
}
How I can get custom claims by typescript like:我如何获得 typescript 的自定义声明,例如:
{
name: "johndoe",
role: "Manager",
}
? ? Thanks.谢谢。
I find a solution:我找到了解决方案:
const token = {
'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name': "johndoe",
'http://schemas.microsoft.com/ws/2008/06/identity/claims/role': "Manager",
'exp': 1525510870,
'iss': "http://localhost:5000",
'aud': "http://localhost:5000"
}
const decodedName = token['http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name']
const decodedRole = token['http://schemas.microsoft.com/ws/2008/06/identity/claims/role']
console.log(decodedName)
console.log(decodedRole)
A JWT if made out of 3 parts, you have to decode the one from the middle, that is the body containing the claims, the first one is a header, and the last one - JWT Signature.一个 JWT 如果由 3 个部分组成,则必须从中间解码一个,即包含声明的主体,第一个是 header,最后一个是 Z1D1FADBD9150349C135781140F 签名。
So, having a token we want to get it's claims, so we decode the part from the middle and parse it to json so we will be able to access claims as fields of that object.因此,有了一个令牌,我们想要获得它的声明,所以我们从中间解码部分并将其解析为 json,这样我们就可以访问声明作为 object 的字段。
let token = localStorage.getItem('token');
let decodedJWT = JSON.parse(window.atob(token.split('.')[1]));
console.log('name: ' + decodedJWT.name);
console.log('role: ' + decodedJWT.role);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.