Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create user in keycloak through keycloak admin client returns IllegalArgumentException

I want to create a user through keycloak admin client but I am getting:

java.lang.IllegalArgumentException: RESTEASY003720: path param realm has not been provided by the parameter map

Here's my bean for keycloak:

@Bean
Keycloak keycloak() {
return KeycloakBuilder
    .builder()
    .serverUrl(localhost:9080/auth)
    .realm(REALM)
    .clientId(CLIENT_ID)
    .username(USERNAME)
    .password(PASSWORD)
    .resteasyClient(new ResteasyClientBuilder().connectionPoolSize(10).build())
    .build();
}

I use this code for calling keycloak:

CredentialRepresentation credentialRepresentation = new 
CredentialRepresentation();
credentialRepresentation.setType(CredentialRepresentation.PASSWORD);
credentialRepresentation.setValue(password);
UserRepresentation userRepresentation = new UserRepresentation();
userRepresentation.setUsername(username);
userRepresentation.setFirstName(firstName);
userRepresentation.setLastName(lastName);
userRepresentation.setEnabled(true);
userRepresentation.setCredentials(
    Arrays.asList(credentialRepresentation));
keycloak.realm(REALM).users().create(userRepresentation);

both keycloak and keycloak admin client are the same version (4.0.0.Final)

My stacktrace looks like this:

java.lang.IllegalArgumentException: RESTEASY003720: path param realm has not been provided by the parameter map at org.jboss.resteasy.specimpl.ResteasyUriBuilder.replaceParameter(ResteasyUriBuilder.java:659) at org.jboss.resteasy.specimpl.ResteasyUriBuilder.buildString(ResteasyUriBuilder.java:581) at org.jboss.resteasy.specimpl.ResteasyUriBuilder.buildFromValues(ResteasyUriBuilder.java:780) at org.jboss.resteasy.specimpl.ResteasyUriBuilder.build(ResteasyUriBuilder.java:772) at org.jboss.resteasy.client.jaxrs.internal.ClientWebTarget.getUri(ClientWebTarget.java:108) at org.jboss.resteasy.client.jaxrs.internal.proxy.ClientInvoker.createRequest(ClientInvoker.java:124) at org.jboss.resteasy.client.jaxrs.internal.proxy.ClientInvoker.invoke(ClientInvoker.java:104) at org.jboss.resteasy.client.jaxrs.internal.proxy.ClientProxy.invoke(ClientProxy.java:76) at com.sun.proxy.$Proxy240.grantToken(Unknown Source) at org.keycloak.admin.client.token.TokenManager.grantToken(TokenManager.java:89) at org.keycloak.admin.client.token.TokenManager.getAccessToken(TokenManager.java:69) at org.mycompany.usermanagement.service.KeycloakService.createUserInSSO(KeycloakService.java:45)

and here is my build.gradle

compile group: 'org.keycloak', name: 'keycloak-admin-client', version: '4.0.0.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'
like image 825
ksadjad Avatar asked Aug 01 '18 05:08

ksadjad


People also ask

What is client role in Keycloak?

Keycloak roles are defined in a dedicated namespace so that all users with the same roles have identical permissions in that namespace. In other words, realm-level roles are a global namespace for a given realm, while client roles are namespaces intended for specific applications.


1 Answers

Are you using Spring Boot?

I have tried your scenario for myself, and works well for me (tested on keycloak 4.0.0 and Spring Boot 1.5.10.RELEASE).

I have done 1 change that the way you create Bean Keycloak (my code is from official docs). Make sure that realm is master and client_id is admin-cli.

@Bean
Keycloak initKeycloakWithAdminRole() {
    return Keycloak.getInstance(
            "http://localhost:8080/auth",
            "master",
            "admin",
            "admin",
            "admin-cli");


My code

Service Code

Controller Code

Depedencies here

like image 155
BlackPearl Avatar answered Sep 23 '22 22:09

BlackPearl