如何通过 id 获取其他用户信息(用户名、名字)?[钥匙斗篷] [英] How can I get other users info(username, firstname) by id? [Keycloak]

查看:112
本文介绍了如何通过 id 获取其他用户信息(用户名、名字)?[钥匙斗篷]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何根据用户 ID 获取用户 keycloak 属性(用户名、名字、电子邮件...)?我在 Keycloak 会话中使用的用户已经分配了 view-users 角色,所以我应该至少可以列出所有用户,是否有任何我可以使用的 Keycloak 类?

我在这里试图实现的是避免将 keycloak 用户数据库复制到另一个本地数据库,但似乎无法访问任何其他用户信息,除了当前会话中的信息...

解决方案

您可以使用 Admin REST API.相关 API 的详细说明可在此处一>.您也可以使用 JAVA 包装器 API.请在下面找到几个示例.

示例 1,REST:

获取访问令牌:

curl \-d "client_id=admin-cli";\-d "用户名=管理员";\-d "密码=秘密";\-d "grant_type=password";\http://localhost:8080/auth/realms/master/protocol/openid-connect/token"

获取所有用户:

curl \-H 授权:持有人 eyJhbGciOiJSUzI...."\http://localhost:8080/auth/admin/realms/master/users"

示例输出:

<预><代码>[{id":349f67de-36e6-4552-ac54-e52085109616",用户名":管理员",启用":真,...},{id":08afb701-fae5-40b4-8895-e387ba1902fb",用户名":lbalev",启用":真,....}]

根据用户 ID 获取用户:

curl \-H 授权:持有人 eyJhbGciOiJSU...."\http://localhost:8080/auth/admin/realms/master/users/349f67de-36e6-4552-ac54-e52085109616"

示例 2,JAVA API:

根据用户 ID 获取用户:

公共类 TestUserAccess {private static final String SERVER_URL = "http://localhost:8080/auth";private static final String REALM = "master";private static final String USERNAME = "admin";private static final String PASSWORD = "secret";private static final String CLIENT_ID = "admin-cli";公共静态无效主(字符串 [] args){Keycloak keycloak = KeycloakBuilder.builder().serverUrl(SERVER_URL).realm(领域).用户名(用户名).密码(密码).clientId(CLIENT_ID).resteasyClient(new ResteasyClientBuilder().connectionPoolSize(10).build()).建造();UsersResource usersResource = keycloak.realm(REALM).users();UserResource userResource = usersResource.get(08afb701-fae5-40b4-8895-e387ba1902fb");System.out.println(userResource.toRepresentation().getEmail());}}

以上示例的相关依赖项是(请注意,版本可能不是最新的):

依赖项{编译组:'org.keycloak',名称:'keycloak-admin-client',版本:'3.3.0.CR2'编译组:'org.jboss.resteasy',名称:'resteasy-jaxrs',版本:'3.1.4.Final'编译组:'org.jboss.resteasy',名称:'resteasy-client',版本:'3.1.4.Final'编译组:'org.jboss.resteasy',名称:'resteasy-jackson2-provider',版本:'3.1.4.Final'}

How can I get user keycloak attributes (username, firstname, email...) based on user id? The user I'm using in the Keycloak session has already the role view-users assigned so I should be able to list at least all users, is there any Keycloak class that I can use?

What I'm trying to achieve here is to avoid to replicate the keycloak users database to another local database, but doesn't seem possible to access any other user info, besides the one in the current session...

解决方案

You can use the Admin REST API. The detailed description of the relevant API is available here. Also you can use the JAVA wrapper API. Please find couple of examples below.

Example 1, REST:

Get an access token:

curl \
  -d "client_id=admin-cli" \
  -d "username=admin" \
  -d "password=secret" \
  -d "grant_type=password" \
  "http://localhost:8080/auth/realms/master/protocol/openid-connect/token"

Get all users:

curl \
  -H "Authorization: bearer eyJhbGciOiJSUzI...." \
  "http://localhost:8080/auth/admin/realms/master/users"

Sample output:

[
     {
        "id":"349f67de-36e6-4552-ac54-e52085109616",
        "username":"admin",
        "enabled":true,
        ...
     },
     {
        "id":"08afb701-fae5-40b4-8895-e387ba1902fb",
        "username":"lbalev",
        "enabled":true,
        ....
     }
  ]

Get a user based by user id:

curl \
  -H "Authorization: bearer eyJhbGciOiJSU...." \
  "http://localhost:8080/auth/admin/realms/master/users/349f67de-36e6-4552-ac54-e52085109616"

Example 2, JAVA API:

Get a user based on user ID:

public class TestUserAccess {

  private static final String SERVER_URL = "http://localhost:8080/auth";
  private static final String REALM = "master";
  private static final String USERNAME = "admin";
  private static final String PASSWORD = "secret";
  private static final String CLIENT_ID = "admin-cli";

  public static void main(String[] args) {

    Keycloak keycloak = KeycloakBuilder
        .builder()
        .serverUrl(SERVER_URL)
        .realm(REALM)
        .username(USERNAME)
        .password(PASSWORD)
        .clientId(CLIENT_ID)
        .resteasyClient(new ResteasyClientBuilder().connectionPoolSize(10).build())
        .build();

    UsersResource usersResource = keycloak.realm(REALM).users();
    UserResource userResource = usersResource.get("08afb701-fae5-40b4-8895-e387ba1902fb");
    System.out.println(userResource.toRepresentation().getEmail());
  }
}

The relevant dependencies for the example above are (please note that the versions might not be up-to-date):

dependencies {
    compile group: 'org.keycloak', name: 'keycloak-admin-client', version: '3.3.0.CR2'
    compile group: 'org.jboss.resteasy', name: 'resteasy-jaxrs', version: '3.1.4.Final'
    compile group: 'org.jboss.resteasy', name: 'resteasy-client', version: '3.1.4.Final'
    compile group: 'org.jboss.resteasy', name: 'resteasy-jackson2-provider', version: '3.1.4.Final'
}

这篇关于如何通过 id 获取其他用户信息(用户名、名字)?[钥匙斗篷]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆