【问题标题】:Keycloak - using admin API to add client role to userKeycloak - 使用管理 API 向用户添加客户端角色
【发布时间】:2019-05-22 10:22:03
【问题描述】:

我正在尝试使用 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-xxxxxxxx' 是创建期间返回的用户 ID,并且存在 create-client 角色

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

【问题讨论】:

    标签: api http-status-code-404 admin keycloak


    【解决方案1】:

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

    【讨论】:

    • 我得到:`“id”:“344e7c81-e7a2-4a43-b013-57d7ed198eee”,“clientId”:“realm-management”,“name”:“${client_realm-management}” ` 那么我应该使用“id”而不是“clientId”作为 URL 参数吗?这给了我 500:/auth/admin/realms/{realm name}/users/e8c113c9-08b9-4600-ac84-45b9d36083b1/role-mappings/clients /344e7c81-e7a2-4a43-b013-57d7ed198eee
    • 是的,您应该使用id 而不是clientId。 500 表示执行操作时发生了一些错误。这可能是不正确的 JSON 或不存在的角色,我不能肯定地说。检查服务器日志。
    • 我还注意到您传递了不正确的 JSON 对象。在这里,您可以获得此特定方法的正确 JSON 结构(您需要 RoleRepresentation 架构):keycloak.org/docs-api/3.0/rest-api/…
    • 您也可以进入管理控制台并打开浏览器调试面板(chrome)的网络选项卡。在那里,您将获得对 API 的所有请求。
    • Endpoint 将一个主体参数“roles”作为 RoleRepresentation 数组(如您所提到的),女巫的所有参数都是可选的,正如文档所说,所以它似乎没问题。另一方面,我已经通过GET /admin/realms/{realm}/users/{id}/role-mappings/clients/{client}/available 成功获得了有关角色的更多信息我已经更新了上面的 JSON,但我仍然得到 500 :(
    猜你喜欢
    • 1970-01-01
    • 2021-12-15
    • 2019-10-19
    • 2023-04-03
    • 2021-11-01
    • 2021-09-06
    • 2019-04-05
    • 2021-10-27
    • 1970-01-01
    相关资源
    最近更新 更多