java - 如何在令牌请求时向 Azure AD 访问令牌 (JWT) 添加自定义声明?
问题描述
Azure AD 中的当前 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"
}
我想添加一些额外的声明,例如departmentId
, someOtherCustomInfo
。我想在我的令牌请求中这样做,没有这个预设。我该怎么做?
目前,我使用 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();
解决方案
Azure AD 发出的 JWT 令牌(无论是访问令牌还是 id 令牌)除了电子邮件地址和其他一些字段外,不包含太多有用的信息。
然后,除了 JWT 令牌中存在的默认声明之外,我们还需要更多声明作为 JWT 令牌的一部分。
我们可以使用自定义声明映射功能。有关更多信息,请通过以下链接
推荐阅读
- angular - 获得响应后在传单上渲染大量动态标记需要大量时间
- c# - EF Core 获取两个属性与列表中的一对属性匹配的记录
- html - React:如何在组件内对齐组件?
- javascript - 如何使jQuery事件回调中的this默认引用jQuery对象
- android - Gridview过滤器在android中不起作用
- python - Discord.py 狙击命令
- visual-studio - Visual Studio 递归折叠解决方案项目
- python - 我可以找到 XPATH
- python-3.x - 从单个图像生成图像数据集
- mysql - MySQL 事务如何在后台工作?MySQL在哪里存储字段的临时值?