-
-
Save abstractj/78b127e8c9273cdcea6eb82a1cfc153c to your computer and use it in GitHub Desktop.
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
<%@ 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> |
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
Are you sure this works for setting the credentials?