简体   繁体   English

如何在令牌请求时向Azure AD访问令牌(JWT)添加自定义声明?

[英]How add custom claim to Azure AD access token (JWT) at token request time?

The current JWT from Azure AD has the following structure: 来自Azure AD的当前JWT具有以下结构:

AzureAD JWT: AzureAD JWT:

{
  "aud": "a5aa555a-aa55-5aaa-5a55-555a5aa55a5a",
  "iss": "https://sts.windows.net/a5aa555a-aa55-5aaa-5a55-555a5aa55a5a/",
  "iat": 1547084136,
  "nbf": 1547084136,
  "exp": 1547089036,
  "acr": "1",
  "aio": "aaaaaaaaaaaaa==",
  "appid": "a5aa555a-aa55-5aaa-5a55-555a5aa55a5a",
  "appidacr": "1",
  "email": "bob@bob.com",
  "idp": "https://sts.windows.net/a5aa555a-aa55-5aaa-5a55-555a5aa55a5a/",
  "ipaddr": "192.168.1.1",
  "name": "Bob Bob",
  "oid": "a5aa555a-aa55-5aaa-5a55-555a5aa55a5a",
  "roles": [],
  "scp": "Directory.AccessAsUser.All User.Read",
  "sub": "a5aa555a-aa55-5aaa-5a55-555a5aa55a5a",
  "tid": "a5aa555a-aa55-5aaa-5a55-555a5aa55a5a",
  "unique_name": "bob@bob.com",
  "uti": "kjkugiugi",
  "ver": "1.0"
}

I want to add in an extra few claims such as departmentId , someOtherCustomInfo . 我想添加一些额外的声明,例如departmentIdsomeOtherCustomInfo I want to do this in my token request, not have this preset. 我想在令牌请求中执行此操作,但没有此预设。 How would I do that? 我该怎么做?

Currently, I use ADAL4J to get the token: 当前,我使用ADAL4J来获取令牌:

//Represents the authority we are asking to provide tokens
AuthenticationContext context = new AuthenticationContext(
    authority,
    true,
    Executors.newFixedThreadPool( numInPool )
);

Future<AuthenticationResult> future = context
    .acquireTokenByAuthorizationCode(
        authCode,
        new URI( redirectUri ),
        credentials,
        resource,
        null
    );

AuthenticationResult authResult = future.get();

//The token
String token = authResult.getAccessToken();

The JWT token emitted by the Azure AD (irrespective of whether it is an access token or an id token) does not contain much useful information except the email address and some other fields. Azure AD发出的JWT令牌(无论它是访问令牌还是id令牌)都没有包含很多有用的信息,除了电子邮件地址和其他一些字段。

Then we need more claims as a part of the JWT token apart from the default claims that are present in the JWT tokens. 然后,除了JWT令牌中存在的默认声明之外,我们还需要更多声明作为JWT令牌的一部分。

We can use Custom Claim mapping feature for the same.For further information please go through below link 我们可以使用自定义声明映射功能。有关更多信息,请通过以下链接

How to: Customize claims emitted in tokens for a specific app in a tenant 如何:针对租户中的特定应用程序自定义令牌发出的声明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM