如何从已装饰的jwt获取索赔参数

时间:2019-08-05 07:04:44

标签: reactjs jwt token decode

我在我的react应用程序中使用jwt-decode解码了一个jwt令牌,但是我无法从中获取Claims参数呢?例如,我将从索取参数中获得角色

这是我的解码令牌结果:

{
  aud: "SampleAudience"
​
  exp: 1564989998
​
  "http://schemas.microsoft.com/ws/2008/06/identity/claims/role": "SuperAdministrators"
​
  "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress": "sso_khani"
​
  "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name": "sso_khani"
​
  "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier": "ab57e777-91b6-4c4e-d709-08d715866555"
​
  iss: "threenine.co.uk"
}

1 个答案:

答案 0 :(得分:0)

您可以要求后端团队在已定义的键名中提供值。 仍然可以做到,

Object.keys(decodedObj).forEach(function (key) {
    let res = key.split("/");
    if (res.length > 1) {
        if (res[length - 1] === 'role') {
            // decodedObj[key] will be your role
        }
        // here you will get role, emailaddress, name, nameidentifier
    }
});

让我知道这是否有帮助。