Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@thomasdarimont
Created January 12, 2016 19:15
Show Gist options
  • Star 24 You must be signed in to star a gist
  • Fork 11 You must be signed in to fork a gist
  • Save thomasdarimont/43689aefb37540624e35 to your computer and use it in GitHub Desktop.
Save thomasdarimont/43689aefb37540624e35 to your computer and use it in GitHub Desktop.
Some Keycloak client examples
package de.tdlabs.training.keycloak;
import static java.util.Arrays.asList;
import javax.ws.rs.core.Response;
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
import org.keycloak.admin.client.Keycloak;
import org.keycloak.admin.client.KeycloakBuilder;
import org.keycloak.representations.idm.CredentialRepresentation;
import org.keycloak.representations.idm.UserRepresentation;
public class KeycloakAdminClientExample {
public static void main(String[] args) throws Exception {
Keycloak kc = KeycloakBuilder.builder() //
.serverUrl("http://localhost:8081/auth") //
.realm("rest-example")//
.username("rest-user-admin") //
.password("password") //
.clientId("admin-cli") //
.resteasyClient(new ResteasyClientBuilder().connectionPoolSize(10).build()) //
.build();
CredentialRepresentation credential = new CredentialRepresentation();
credential.setType(CredentialRepresentation.PASSWORD);
credential.setValue("test123");
credential.setTemporary(false);
UserRepresentation user = new UserRepresentation();
user.setUsername("testuser");
user.setFirstName("Test");
user.setLastName("User");
user.setCredentials(asList(credential));
user.setEnabled(true);
user.setRealmRoles(asList("admin"));
// Create testuser
Response result = kc.realm("rest-example").users().create(user);
if (result.getStatus() != 201) {
System.err.println("Couldn't create user.");
System.exit(0);
}
System.out.println("Testuser created.... verify in keycloak!");
System.out.println("Press any key...");
System.in.read();
// Delete testuser
String locationHeader = result.getHeaderString("Location");
String userId = locationHeader.replaceAll(".*/(.*)$", "$1");
kc.realm("rest-example").users().get(userId).remove();
}
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.tdlabs</groupId>
<artifactId>keycloak-training-rest-api-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<keycloak.version>1.8.0.CR1-SNAPSHOT</keycloak.version>
<resteasy.version>3.0.9.Final</resteasy.version>
</properties>
<dependencies>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-admin-client</artifactId>
<version>${keycloak.version}</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>jaxrs-api</artifactId>
<version>${resteasy.version}</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
<version>${resteasy.version}</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson-provider</artifactId>
<version>${resteasy.version}</version>
</dependency>
</dependencies>
</project>
@alvaroqv
Copy link

Hi @thomasdarimont thanks, I'm using userRessource.get(userId).executeActionsEmail to send the email. I don't know if it is the best solution but it is working.

@amit-tarento
Copy link

Hi @thomasdarimont thanks for client example. Do you know , how to get the exact validation message in failed user creation like email already exist, or username already exist or password pattern does not match.

@maherBoudhraa
Copy link

Hello @thomasdarimont thanks for this client example. When i try to run it i get a HTTP 400 Bad Request can you help me ??

@chittaa
Copy link

chittaa commented Apr 26, 2018

I was receiving jackson error while running this sample. It got resolved when i used - <artifactId/>resteasy-jackson2-provide</artifactId>
Kc version - 3.4.3.Final

@Stalluri1
Copy link

hey this is giving me this error - (default task-1) Uncaught server error: java.lang.IllegalArgumentException: interface org.keycloak.admin.client.token.TokenService is not visible from class loader
any help ?

@charStarGoulash
Copy link

Is there anyway to use the Client API not the REST ADMIN API and still be able to use a JSON config to create a new realm as we would with a POST to the RestAdminAPI?

@VenkateshGuntuk
Copy link

Realm Role not getting created....i think user.setRealmRole("rolename") not working

@thomasdarimont
Copy link
Author

Take a look at this example: https://gist.github.com/thomasdarimont/c4e739c5a319cf78a4cff3b87173a84b
Roles need to be set explicitly.

@canattofilipe
Copy link

It runs outside server ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment