gpt4 book ai didi

token 中的 KeyCloak 组 ID 丢失

转载 作者:行者123 更新时间:2023-12-04 10:48:05 25 4
gpt4 key购买 nike

我目前正在设置一个新的 KeyCloak 实例,我正试图实现以下目标:用户将被放置在- 团体- 这些组将获得客户特定的角色

例如,我有角色“Publishers”和几组发布者:Publisher1、Publisher2、...

因此,当用户登录时,我可以确定他是否是发布者,然后授予他访问网站上一组特定功能的权限。然后小组将缩小他将收到的所有信息。

就像该角色将授予他访问 REST API 的权限,并且该组将过滤他将收到的结果。

在 SQL 中:SELECT * FROM xyz where publisher_id = ?

在 token 中我想查看这些信息。使用评估功能时,我目前收到:

{
"jti": "3e96fc9d-b1dc-428a-8f8e-0661f9cf265b",
"exp": 1578303161,
"nbf": 0,
"iat": 1578302861,
"iss": "https://prodo-sso-ti.ariva-services.de/auth/realms/PRODO",
"aud": "account",
"sub": "55bed571-dd3b-4282-8688-5da543517a49",
"typ": "Bearer",
"azp": "dashboard",
"auth_time": 0,
"session_state": "12ab2b8c-dc9a-42ca-b106-1a213dd38fc0",
"acr": "1",
"allowed-origins": [
"https://secretlink"
],
"realm_access": {
"roles": [
"offline_access",
"uma_authorization"
]
},
"resource_access": {
"account": {
"roles": [
"manage-account",
"manage-account-links",
"view-profile"
]
},
"dashboard": {
"roles": [
"Publisher"
]
}
},
"scope": "openid profile email",
"group_membership": [
"/Publisher1"
],
"email_verified": true,
"name": "My Name",
"preferred_username": "mb",
"locale": "de",
"given_name": "My",
"family_name": "Name",
"email": "my@name.de"
}

我激活了组成员映射器以获取用户所在的组。问题是,我只得到组的名称,而我需要更有用的东西,比如 ID。因此,我尝试将属性添加到数值为“1”的组“publisher_id”。

如何在组成员信息或其他地方也获得此 publisher_id。或者也许我走错了路,这可以以某种不同的方式实现?

我很感激任何提示:)

最佳答案

有一种简单的方法可以将 Groups Id 添加到 token 中:

  1. 为您的客户创建一个新的客户范围:

Clients Scopes -> Create -> Client Scope Template(Audience template) -> your_client_name

  1. 在新的Client Scope中创建一个新的Mapper

Clients Scopes -> your_client_name -> Mappers -> Create

  1. 设置一些名称,Mapper Type必须是Script Mapper

然后将此代码粘贴到脚本部分:

/**
* Available variables:
* user - the current user
* realm - the current realm
* token - the current token
* userSession - the current userSession
* keycloakSession - the current userSession
*/


//insert your code here...
var groups = [];
for each (var group in user.getGroups()) groups.push(group.getId());
token.setOtherClaims("groups_ids",
Java.to(groups, "java.lang.String[]")
);

不要忘记设置添加到访问 token

您将在您的 token 中看到它: groups_ids

关于 token 中的 KeyCloak 组 ID 丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59609731/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com