Skip to content

Instantly share code, notes, and snippets.

@abstractj
Created July 20, 2016 20:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abstractj/78b127e8c9273cdcea6eb82a1cfc153c to your computer and use it in GitHub Desktop.
Save abstractj/78b127e8c9273cdcea6eb82a1cfc153c to your computer and use it in GitHub Desktop.
<%@ page import="org.keycloak.admin.client.Keycloak" %>
<%@ page import="org.keycloak.admin.client.resource.ClientsResource" %>
<%@ page import="org.keycloak.representations.idm.ClientRepresentation" %>
<%@ page import="org.keycloak.common.util.UriUtils" %>
<%@ page import="org.keycloak.admin.client.resource.RealmResource" %>
<%@ page import="org.keycloak.representations.idm.UserRepresentation" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="org.keycloak.representations.idm.CredentialRepresentation" %>
<%@ page import="java.util.LinkedList" %>
<%@ page import="org.keycloak.representations.idm.RoleRepresentation" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" %>
<%@ page session="false" %>
<html>
<head>
<title>Add user example</title>
</head>
<body>
<%
String authServer = UriUtils.getOrigin(request.getRequestURL().toString()) + "/auth";
Keycloak keycloak = Keycloak.getInstance(authServer, "master", "admin", "password", "admin-cli");
RealmResource realm = keycloak.realm("master");
UserRepresentation user = new UserRepresentation();
user.setEnabled(true);
user.setUsername("steve");
user.setFirstName("Steve");
user.setLastName("Vai");
RoleRepresentation roleRepresentation = new RoleRepresentation();
roleRepresentation.setName("newRole");
roleRepresentation.setDescription("My new role");
realm.roles().create(roleRepresentation);
if (user.getRealmRoles() == null) {
user.setRealmRoles(new ArrayList<String>());
}
//Add roles
user.getRealmRoles().add("newRole");
//Set credentials
CredentialRepresentation credential = new CredentialRepresentation();
credential.setType(CredentialRepresentation.PASSWORD);
credential.setValue("password");
if (user.getCredentials() == null) {
user.setCredentials(new LinkedList<CredentialRepresentation>());
}
user.getCredentials().add(credential);
realm.users().create(user);
%>
<br><br>
</body>
</html>
@pires
Copy link

pires commented Aug 18, 2016

Are you sure this works for setting the credentials?

@lrozenblyum
Copy link

We cannot provide hashedSaltedValue for the password here, due to the issue: https://issues.jboss.org/browse/KEYCLOAK-7801

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