我正在尝试使用 keycloak AdminAPI (https://www.keycloak.org/docs-api/3.0/rest-api/index.html#_users_resource) 来创建用户并分配客户端角色。我收到了正确的令牌,并且创建了用户,但分配角色返回 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-xxxxxxx' 是创建过程中返回的用户 ID,且 create-client 角色存在
我需要一种通过 Http 请求添加客户端角色的方法。我看到有一些 java 的 keycloack 实现,但我使用的是 .NET CORE,所以会有目标实现,但我需要首先有工作请求,正如你可能猜到的那样
您必须将客户端 UUID 传递给
role-mappings
REST 方法,而不是您在管理 UI 中创建客户端时指定的 ID。使用 GET /admin/realms/{realm}/clients?clientId=realm-management
REST 方法找出客户端 UUID。
更新
在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"
}
]
如果其他人最终来到这里,因为他们从 KeyCloak 获得 500 尝试使用 PostMan 调用以下 API 将用户添加到角色
{{HOST_NAME}}/auth/admin/realms/{{REALM_NAME}}/users/{{POSTMAN_USER_ID}}/角色映射/领域
那么你需要
我在 PostMan 中遗漏了 Content-Length,那就是我得到 500 的时候。大概这至少是一个文档错误,但也是一个产品错误,因为它应该 a) 不需要长度,b) 在以下情况下不给出 500:没有发送。