Created
December 11, 2017 17:12
-
-
Save celsoagra/35844907818e68359bd268395a7c84f6 to your computer and use it in GitHub Desktop.
Add users on Keycloak 3.1.0.Final
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
public class KeycloakManager { | |
private Keycloak keycloak; | |
/** | |
* Singleton method | |
* @param properties KeycloakProperties is a pojo to store configuration info | |
*/ | |
public static KeycloakManager getInstance(KeycloakProperties properties) { | |
KeycloakManager manager = new KeycloakManager(properties); | |
return manager; | |
} | |
/** | |
* Constructor with Keycloak Builder | |
* @param properties KeycloakProperties is a pojo to store configuration info | |
*/ | |
private KeycloakManager(KeycloakProperties properties) { | |
this.keycloak = KeycloakBuilder.builder().serverUrl(properties.getKeycloakUrl()) | |
.realm(properties.getKeycloakMasterRealm()).clientId(properties.getMasterClient()) | |
.username(properties.getKeycloakAppUsername()).password(properties.getPassword()) | |
.resteasyClient(new ResteasyClientBuilder().connectionPoolSize(10).build()).build(); | |
} | |
/** | |
* Method to create an user and | |
* @param properties KeycloakProperties is a pojo to store configuration info | |
*/ | |
public Response createUser(UserKeycloak userKeycloak) { | |
UserRepresentation user = new UserRepresentation(); | |
user.setUsername(userKeycloak.getUsername()); | |
user.setFirstName(userKeycloak.getFirstName()); | |
user.setLastName(userKeycloak.getLastName()); | |
user.setEnabled(true); | |
if (userKeycloak.getEmail() != null) { | |
user.setEmail(userKeycloak.getEmail()); | |
} | |
RealmResource realmResource = keycloak.realm(properties.getAppRealm()); | |
UsersResource usersResourceAll = realmResource.users(); | |
return usersResourceAll.create(user); | |
} | |
} |
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
<?xml version="1.0" encoding="UTF-8"?> | |
<project | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" | |
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> | |
<!-- | |
... | |
--> | |
<properties> | |
<keycloak.version>3.1.0.Final</keycloak.version> | |
</properties> | |
<dependencies> | |
<dependency> | |
<groupId>org.keycloak</groupId> | |
<artifactId>keycloak-admin-client</artifactId> | |
<version>${keycloak.version}</version> | |
</dependency> | |
<dependency> | |
<groupId>org.keycloak</groupId> | |
<artifactId>keycloak-authz-client</artifactId> | |
<version>${keycloak.version}</version> | |
</dependency> | |
<dependency> | |
<groupId>org.jboss.resteasy</groupId> | |
<artifactId>resteasy-jaxrs</artifactId> | |
<version>3.1.3.Final</version> | |
</dependency> | |
<dependency> | |
<groupId>org.jboss.resteasy</groupId> | |
<artifactId>resteasy-multipart-provider</artifactId> | |
<version>3.1.3.Final</version> | |
</dependency> | |
<dependency> | |
<groupId>javax.ejb</groupId> | |
<artifactId>javax.ejb-api</artifactId> | |
<version>3.2</version> | |
</dependency> | |
<dependency> | |
<groupId>org.jboss.resteasy</groupId> | |
<artifactId>resteasy-jackson2-provider</artifactId> | |
<version>3.1.3.Final</version> | |
</dependency> | |
<dependency> | |
<groupId>org.jboss.resteasy</groupId> | |
<artifactId>resteasy-jackson-provider</artifactId> | |
<version>3.1.3.Final</version> | |
</dependency> | |
<dependency> | |
<groupId>org.jboss.resteasy</groupId> | |
<artifactId>resteasy-client</artifactId> | |
<version>3.1.3.Final</version> | |
</dependency> | |
</dependencies> | |
<!-- | |
... | |
--> | |
</project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment