[keycloak-user] Listing users with a specific role and group through the admin client on springboot

Niko Köbler niko at n-k.de
Tue Mar 19 01:41:16 EDT 2019


Hi Vikram,

when getting a user list from keycloak, groups and roles are empty, that's normal behavior, not a bug.
If you want to get groups and roles for a user, you have to retrieve details for a single user from keycloak.
As this might become much api calls to the server, this is not a good solution in most scenarios.

In your case, I suggest to get also a list/set of users for the specific group you are looking for and then match the users in your role-set to the users of the group-set and use the intersection of both.

Regards,
- Niko



> Am 18.03.2019 um 13:19 schrieb Vikram <vikram.eswar at fleetroute.com>:
> 
> Does someone have anything on this ?
> 
> Regards,
> 
> Vikram
> 
> On 3/15/2019 3:02 PM, Vikram wrote:
>> Hi all,
>> 
>> Versions in use:
>> 
>>       Springboot version : 2.1.3 FINAL
>> 
>>       Keycloak version : 4.8.2
>> 
>>       Springboot adapter version: 4.8.3 FINAL
>> 
>>       Keycloak admin client 4.8.2 FINAL
>> 
>> So I am trying to get all the users that have a role "customer" and
>> belong to a group "group1".
>> 
>> I am using the following code.
>> 
>> RoleResource roleResource = realmResource.roles().get("customer");
>> Set<UserRepresentation> customers= roleResource.getRoleUserMembers();
>> ArrayList<UserRepresentation> groupCustomers = new ArrayList<UserRepresentation>();
>> 
>> for (UserRepresentation user: customers) {
>>          if (user.getGroups().contains("group1") { //error
>>              System.out.println("group customer: " + user.getUsername());
>>              groupCustomers.add(user);
>>           }
>> }
>> 
>> However, I get an error when I loop through the user representations to
>> read the group names. I do not get the group and roles information. I
>> get the username, first name and last name though.. Is it a permission
>> issue ? How can I get around it ?
>> 
>> Regards,
>> Vikram
>> 
>> _______________________________________________
>> keycloak-user mailing list
>> keycloak-user at lists.jboss.org
>> https://lists.jboss.org/mailman/listinfo/keycloak-user
>> 
>> 
> _______________________________________________
> keycloak-user mailing list
> keycloak-user at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/keycloak-user




More information about the keycloak-user mailing list