Last active
June 11, 2023 08:54
-
-
Save ThomasVitale/b1f9b166277721582ce78ff1f7ec5873 to your computer and use it in GitHub Desktop.
Keycloak Admin REST API for Java
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
group 'com.thomasvitale.keycloak-demo' | |
version '1.0-SNAPSHOT' | |
apply plugin: 'java' | |
sourceCompatibility = 1.8 | |
repositories { | |
mavenCentral() | |
} | |
dependencies { | |
compile group: 'org.keycloak', name: 'keycloak-admin-client', version: '3.3.0.CR2' | |
//compile group: 'org.keycloak', name: 'keycloak-admin-client', version: '3.2.1.Final' | |
compile group: 'org.jboss.resteasy', name: 'resteasy-jaxrs', version: '3.1.4.Final' | |
compile group: 'org.jboss.resteasy', name: 'resteasy-client', version: '3.1.4.Final' | |
compile group: 'org.jboss.resteasy', name: 'resteasy-jackson2-provider', version: '3.1.4.Final' | |
testCompile group: 'junit', name: 'junit', version: '4.12' | |
} |
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
import com.sun.org.apache.regexp.internal.RE; | |
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder; | |
import org.keycloak.admin.client.Keycloak; | |
import org.keycloak.admin.client.KeycloakBuilder; | |
import org.keycloak.admin.client.resource.UserResource; | |
import org.keycloak.admin.client.resource.UsersResource; | |
import org.keycloak.representations.idm.CredentialRepresentation; | |
import org.keycloak.representations.idm.UserRepresentation; | |
import java.util.Arrays; | |
public class Main { | |
private static final String SERVER_URL = "http://localhost:8180/auth"; | |
private static final String REALM = "demo"; | |
private static final String USERNAME = "dumbledore"; | |
private static final String PASSWORD = "password"; | |
private static final String CLIENT_ID = "demo-client"; | |
public static void main(String[] args) { | |
Keycloak keycloak = KeycloakBuilder | |
.builder() | |
.serverUrl(SERVER_URL) | |
.realm(REALM) | |
.username(USERNAME) | |
.password(PASSWORD) | |
.clientId(CLIENT_ID) | |
.resteasyClient(new ResteasyClientBuilder().connectionPoolSize(10).build()) | |
.build(); | |
CredentialRepresentation credentialRepresentation = new CredentialRepresentation(); | |
credentialRepresentation.setType(CredentialRepresentation.PASSWORD); | |
credentialRepresentation.setValue("12345678"); | |
UserRepresentation userRepresentation = new UserRepresentation(); | |
userRepresentation.setUsername("lupin"); | |
userRepresentation.setFirstName("Remus"); | |
userRepresentation.setLastName("Lupin"); | |
userRepresentation.setEnabled(true); | |
userRepresentation.setCredentials(Arrays.asList(credentialRepresentation)); | |
//keycloak.realm(REALM).users().create(userRepresentation); | |
UsersResource usersResource = keycloak.realm(REALM).users(); | |
UserRepresentation user = usersResource.search("lupin").get(0); | |
user.setEmail("[email protected]"); | |
usersResource.get(user.getId()).update(user); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://stackoverflow.com/a/60471582/10311582 this helped me for the same issue.