如何在Keycloak中搜索组内的用户?

kos*_*ych 3 java keycloak keycloak-rest-api

使用Keycloak 11.0.3。我尝试使用 Keycloak API 搜索组内的用户:

List<UserRepresentation>  users = realmResource.users().search(username, firstname, lastname, email,
    0, 100);
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试获取找到的用户组时,即使用户有组,我也会得到 null:

List<String> groups = users.get(0).getGroups(); //It's null
Run Code Online (Sandbox Code Playgroud)

那么如何搜索群组内的用户呢?

dre*_*ash 7

要获取用户所属的组,请尝试以下操作:

realm.users().get(userId).groups();
Run Code Online (Sandbox Code Playgroud)

请改用用户 ID。

要获取给定组的用户,请执行以下操作:

realm.groups().group(groupId).members();
Run Code Online (Sandbox Code Playgroud)