Java keycloak RealmResource searchByAttributes exact query

73 Views Asked by At

I want to get user by cnp, but might be the case when cnp is empty string, and in this case with this API searchByAttributes it returns me all users, but I want exact search query.

public Optional<UserKeycloak> getUserKeycloakByCNP(String cnp) {
    UsersResource usersResource = realmResource.users();

    String searchQuery = "cnp:" + cnp;

    List<UserRepresentation> matchingUsers = usersResource
            .searchByAttributes(searchQuery);

    if (!matchingUsers.isEmpty()) {
        UserRepresentation userRepresentation = matchingUsers.get(0);
        return Optional.of(getUserKeycloak(usersResource, userRepresentation));
    } else {
        return Optional.empty();
    }
}
0

There are 0 best solutions below