[keycloak-user] 401 when try to create an user
Fabio Ebner
fabio.ebner at lumera.com.br
Fri Oct 12 17:55:35 EDT 2018
I trying to use the keycloak admin client to create one user:
my keycloak user are: keycloak and password are k3ycl0 at k
So in using this code:
String serverUrl = "http://localhost:9090/auth/admin";
String realm = "Lumera";
String clientId = "admin-cli";
String clientSecret = "b1bc8d8e-2cff-4d6d-9cbd-d5fcfe82d1bc";
Keycloak keycloak = KeycloakBuilder.builder() //
.serverUrl(serverUrl)
.realm(realm)
.clientId(clientId)
// .clientSecret(clientSecret)
.username("keycloak")
.password("k3ycl0 at k")
.build();
UserRepresentation user = new UserRepresentation();
user.setEnabled(true);
user.setUsername("tester1");
user.setEmail("te at teste.com.br");
user.setAttributes(Collections.singletonMap("origin",
Arrays.asList("demo")));
RealmResource realmResource = keycloak.realm(realm);
UsersResource userRessource = realmResource.users();
javax.ws.rs.core.Response response = userRessource.create(user);
System.out.println(response.getLocation());
String userId =
response.getLocation().getPath().replaceAll(".*/([^/]+)$", "$1");
RoleRepresentation testerRealmRole =
realmResource.roles().get("tester").toRepresentation();
userRessource.get(userId).roles().realmLevel().add(Arrays.asList(testerRealmRole));
ClientRepresentation app1Client =
realmResource.clients().findByClientId("app1").get(0);
RoleRepresentation userClientRole =
realmResource.clients().get(app1Client.getId()).roles().get("user").toRepresentation();
userRessource.get(userId).roles().clientLevel(app1Client.getId()).add(Arrays.asList(userClientRole));
CredentialRepresentation passwordCred = new
CredentialRepresentation();
passwordCred.setTemporary(false);
passwordCred.setType(CredentialRepresentation.PASSWORD);
passwordCred.setValue("test");
userRessource.get(userId).resetPassword(passwordCred);
But returns: Caused by: javax.ws.rs.NotAuthorizedException: HTTP 401
Unauthorized in line javax.ws.rs.core.Response response =
userRessource.create(user);
I using springboot 1.5.16
More information about the keycloak-user
mailing list