4
votes

I am using Spring Boot, Keycloak 10, java 8 and keycloak-admin-client jar. I am able to get user, his groups and roles.

When it comes to search I see different search method options for example I could :

 List<UserRepresentation> search = getKeycloakInstance().realm("my-realm").users()
                .search("username");

https://www.keycloak.org/docs-api/10.0/javadocs/org/keycloak/admin/client/resource/UsersResource.html

But what i need to do i to write couple of methods:

  • search by roles (so search users who has some roles)

  • search by groups and group attributes

  • search by text (firstname, lastname, email) in 'contains' manner: mytext

  • search by roles and text

  • search by list of ids (uuids of users)

I dont' see such possibilities in keycloak-admin-client, or it is possible of what else should I use instead of keycloak-admin-client ?

1
You'll probably need to build up your custom code againt the keycloak API: keycloak.org/docs-api/10.0/rest-api/index.htmlXtreme Biker

1 Answers

4
votes

Unfortunately, keycloak-admin-client doesn't provide lots of search options.

How to find users by role:

RoleResource roleResource = getKeycloakInstance().realm("realm_name")
                          .roles().get("role_name");  
roleResource.getRoleUserMembers();

How to find all users in the group:

getKeycloakInstance().realm("realm_name").groups().group("your_group").members();

How to find users by username, firstName, lastName, email:

getKeycloakInstance().realm("my-realm").users()
            .search("username", "lastName", "email");

If it's okay for you, try to use Keycloak Admin REST API to get more search opportunities.