java - 我们可以通过发送包含 2 个以上用户信息的 json 数组在 Keycloak 中创建用户吗?

标签 java keycloak keycloak-services

我能够通过使用 postman 发布仅包含 1 个用户的 json 在 Keycloak 中创建用户。

http://localhost:8080/auth/admin/realms/master/users

但是当我试图通过传递超过 1 条记录的 json 数组来创建超过 1 个用户时,我收到 500 内部服务器错误

[
  {
    "username": "user1",
    "firstName": "John",
    "attributes": {
      "pl_uid": null
    },
    "credentials": [
      {
        "temporary": true,
        "type": "password",
        "value": "ares2012"
      }
    ]
  },
  {
    "username": "1000195",
    "firstName": "Matt",
    "attributes": {
      "pl_uid": null
    },
    "credentials": [
      {
        "temporary": true,
        "type": "password",
        "value": "rx3o0t9f"
      }
    ]
  }
]

有什么方法可以将 json 数组发送到 keycloak 并在那里创建用户?

最佳答案

请查看Keycloak邮件列表中添加的讨论

Just double checked the approach I suggested. I thought we had made it possible to import users into an existing realm, but that's not the case. You have to create the whole realm. It's still possible to do it this way, first create the realm and add an example user. Stop the server and run it again with:

 bin/standalone.sh -Dkeycloak.migration.action=export -Dkeycloak.migration.provider=dir -Dkeycloak.migration.realmName=<realm name> -Dkeycloak.migration.dir=<dir name>
  • Replace realm name and dir name

In dir name you should then get a few json files. You can then update realm name-users-0.json to add the users you want to import.

As Bill points out the admin client could be a good alternative approach. We also have a Java client that makes it simpler to use. Have a look at the admin-client example.

所以这个 URL 可以提供帮助。看看this link

另一种选择是使用 partialImport API 导入用户(使用管理员用户 token ):

access_token=`curl --data "grant_type=password&username=admin&password=admin&client_secret=secret&client_id=admin-cli" http://localhost:8180/auth/realms/master/protocol/openid-connect/token| jq -r .access_token`

curl -X POST -H 'Accept: application/json' -H 'Content-Type: application/json' -H "Authorization: Bearer $access_token"  --data "@$PWD/myrealm-users-0.json"  http://localhost:8180/auth/admin/realms/myrealm/partialImport

关于java - 我们可以通过发送包含 2 个以上用户信息的 json 数组在 Keycloak 中创建用户吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54512217/

相关文章:

javascript - 如何使用 Keycloak 获取当前用户头像?

java - 从 RequestBuilder 访问 JSON 响应

keycloak - 是否可以在 Keycloak 中为每个用户添加多于一封电子邮件?

java - DBUnit 设置方法未调用?

spring - Keycloak Spring Multi-Tenancy 空指针异常

spring-boot - 无法使用 KeycloakAutoConfiguration 使用 Spring Boot Security UnsatisfiedDependencyException 设置 Keycloak

jboss - Keycloak:从用户个人资料页面返回

python-3.x - 通过 REST API 创建 Keycloak 客户端

java - 使用参数作为数组从 PHP 调用 Java

java - Android ViewModel 到模型事件