简体   繁体   English

@auth0/angular-jwt:从解码的令牌中获取声明

[英]@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.

相关问题 使用 Regexp 配置时,JWT 令牌未发送到 auth0/angular-jwt 中的 allowedDomain 或 whitelistedDomains - JWT Token not sent to allowedDomain or whitelistedDomains in auth0/angular-jwt when using Regexp config 找不到模块“@auth0/angular-jwt/” - Cannot find module "@auth0/angular-jwt/" angular2-jwt中的AuthHttp类不存在于'npm i @ auth0 / angular-jwt'中 - AuthHttp class in angular2-jwt not present in 'npm i @auth0/angular-jwt' 目标入口点“@auth0/angular-jwt”中的错误缺少依赖项: - ERROR in The target entry-point “@auth0/angular-jwt” has missing dependencies: 模块“ node_modules / @ auth0 / angular-jwt / index”没有导出成员“ JwtHelper” - Module “node_modules/@auth0/angular-jwt/index”' has no exported member 'JwtHelper' node_modules/@auth0/angular-jwt/lib/jwtoptions.token.d.ts(2,50) 中的错误:错误 TS2304:找不到名称“未知” - ERROR in node_modules/@auth0/angular-jwt/lib/jwtoptions.token.d.ts(2,50): error TS2304: Cannot find name 'unknown' Ionic + Angular:Auth0 JWT 不在标头上发送令牌 - Ionic + Angular: Auth0 JWT not sending token on headers auth0与angular2-如何获取令牌 - auth0 with angular2 - how to get token Angular6 angular-jwt 不向请求标头添加令牌 - Angular6 angular-jwt not adding token to request headers angular-jwt令牌未在我的http请求标头中发送 - angular-jwt token does not get sent in my http request headers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM