gpt4 book ai didi

api - Keycloak - 使用管理 API 向用户添加客户端角色

转载 作者:行者123 更新时间:2023-12-01 11:13:39 27 4
gpt4 key购买 nike

我正在尝试使用 keycloak AdminAPI ( https://www.keycloak.org/docs-api/3.0/rest-api/index.html#_users_resource ) 创建用户并分配客户端角色。我收到正确的 token ,并且创建了用户但分配角色返回 404

我正在使用 Postman 连接 API:

/auth/realms/{realmName}/protocol/openid-connect/token
Content-Type application/x-www-form-urlencoded <-with parameters ofc
/auth/admin/realms/{realmName}/users

Content-Type application/json
Authorization Bearer {TOKEN}
Body:
{
"username": "name",
"enabled": true,
"emailVerified": false,
"firstName": "first",
"lastName": "last",
"credentials": [
{
"type": "password",
"value": "newPas1*",
"temporary": false
}
]
}

以上对我有用,但下一个不行
/auth/admin/realms/{realmName}/users/xxxxxx-xxxx-xxxx-xxxx-xxxxxxxx/role-mappings/clients/realm-management

Content-Type application/json
Authorization Bearer {TOKEN}
Body:
{
"roles": [
{
"id": "0830ff39-43ea-48bb-af8f-696bc420c1ce",
"name": "create-client",
"description": "${role_create-client}",
"composite": false,
"clientRole": true,
"containerId": "344e7c81-e7a2-4a43-b013-57d7ed198eee"
}
]
}

其中 'xxxxxx-xxxx-xxxx-xxxx-xxxxxxxx' 是创建过程中返回的用户 ID,并且存在创建客户端角色

我需要一种通过 Http 请求添加客户端角色的方法。我看到有一些 java 的 keycloack 实现,但我使用的是 .NET CORE,所以会有目标实现,但我需要先有工作请求,你可能会猜到

最佳答案

您必须将客户端 UUID 传递给 role-mappings REST 方法,而不是您在管理 UI 中创建客户端时指定的 ID。使用 GET /admin/realms/{realm}/clients?clientId=realm-management用于找出客户端 UUID 的 REST 方法。

更新

在 Keycloak 6.0.1 中添加角色需要传递角色名称和 id。

例子:

POST /auth/admin/realms/{realm}/users/{user}/role-mappings/clients/{client}

[
{
"id": "0830ff39-43ea-48bb-af8f-696bc420c1ce",
"name": "create-client"
}
]

关于api - Keycloak - 使用管理 API 向用户添加客户端角色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56254627/

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