The goal of this library is to create a Keycloak Admin Client library that is not dependent on the Resteasy JAX-RS implementations, and just uses a simple wrapper on the Apache HttpClient and Jackson libraries.
- Implements an alternative to the org.keycloak.admin.client.Keycloak and KeycloakBuilder classes that does not use JAX-RS or Resteasy
- The TokenManager and TokenService are also reimplemented not to use JAX-RS or Resteasy.
- Uses the io.phasetwo.keycloak.admin.Http wraper to make a concrete implementation of all interfaces in the org.keycloak.admin.client.resource package.
- All dependencies are declared as
optionalin case you are using this from within Keycloak (our use case).
import io.phasetwo.keycloak.admin.Keycloak;
import io.phasetwo.keycloak.admin.KeycloakBuilder;
import org.keycloak.OAuth2Constants;
Keycloak keycloak =
KeycloakBuilder.builder()
.serverUrl("https://sso.example.com")
.realm("master")
.grantType(OAuth2Constants.CLIENT_CREDENTIALS)
.clientId("admin-cli")
.clientSecret("my-client-secret")
.build();import io.phasetwo.keycloak.admin.Keycloak;
import io.phasetwo.keycloak.admin.KeycloakBuilder;
import org.keycloak.OAuth2Constants;
Keycloak keycloak =
KeycloakBuilder.builder()
.serverUrl("https://sso.example.com")
.realm("master")
.grantType(OAuth2Constants.PASSWORD)
.username("admin")
.password("admin-password")
.clientId("admin-cli")
.build();import org.keycloak.representations.idm.RealmRepresentation;
RealmRepresentation realm = keycloak.realm("my-realm").toRepresentation();
System.out.println("Realm: " + realm.getRealm());import java.util.List;
import org.keycloak.representations.idm.UserRepresentation;
List<UserRepresentation> users = keycloak.realm("my-realm").users().search("alice");
if (!users.isEmpty()) {
UserRepresentation user = users.get(0);
String userId = user.getId();
UserRepresentation fullUser = keycloak.realm("my-realm").users().get(userId).toRepresentation();
fullUser.setFirstName("Alice Updated");
keycloak.realm("my-realm").users().get(userId).update(fullUser);
}keycloak.close();Compile the project:
mvn clean compileBuild the jar:
mvn clean packageRun all tests:
mvn testRun only the Keycloak Testcontainers integration test:
mvn -Dtest=KeycloakContainerIT test- The integration test (
KeycloakContainerIT) usestestcontainers-keycloak. - Docker must be running and available on the host.