问题描述
我正在尝试使用 keycloak AdminAPI (https://www.keycloak.org/docs-api/3.0/rest-api/index.html#_users_resource) 创建用户并分配客户端角色.我收到了正确的令牌,并且创建了用户但分配角色返回 404
I'm triyng to use keycloak AdminAPI (https://www.keycloak.org/docs-api/3.0/rest-api/index.html#_users_resource) to create user and assign client roles. I'm receiving correct token, and user is created but assigning roles return 404
我正在使用 Postman 连接 API:
I'm using Postman to connect with 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
}
]
}
以上对我有用,但下一个不行
Above works for me, but the next one don't
/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' 是创建时返回的 userID,并且存在 create-client 角色
where 'xxxxxx-xxxx-xxxx-xxxx-xxxxxxxx' is userID returned during creation and create-client role exists
我需要一种通过 Http 请求添加客户端角色的方法.我看到 Java 有一些 keycloack 实现,但我使用的是 .NET CORE,所以会有目标实现,但我需要先有工作请求,你可能会猜到
I need a way to add client role via Http request. I saw there are some keycloack implementation for java but I'm using .NET CORE so there will be the target implementation but I need to have working request first as you may gues
推荐答案
您必须将客户端 UUID 传递给 role-mappings
REST 方法,而不是您在 admin 中创建客户端时指定的 ID用户界面.使用 GET/admin/realms/{realm}/clients?clientId=realm-management
REST 方法找出客户端 UUID.
You have to pass client UUID to the role-mappings
REST method, not the ID that you specify when creating a client in admin UI. Use GET /admin/realms/{realm}/clients?clientId=realm-management
REST method to find out the client UUID.
更新
在 Keycloak 6.0.1 中添加角色需要传递角色名称和 id.
In Keycloak 6.0.1 to add a role it is required to pass role name and id.
示例:
POST /auth/admin/realms/{realm}/users/{user}/role-mappings/clients/{client}
[
{
"id": "0830ff39-43ea-48bb-af8f-696bc420c1ce",
"name": "create-client"
}
]
这篇关于Keycloak - 使用管理 API 向用户添加客户端角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!