Forked from thomasdarimont/KeycloakAdminClientExample.java
Created
August 6, 2018 14:24
-
-
Save TheGeekPharaoh/c3e5597ca005bd7a2ac23ec51309a0ce to your computer and use it in GitHub Desktop.
Some Keycloak client examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment