I'm having a problem trying to assign role to user via keycloak admin client. It always returns bad request. First I created a new realm in keycloak with new client. I created couple of users and assigned all roles from realm-management client to one of the users. I used credentials of that user in KeycloakBuilder.
Now admin client can create users, but assigning role to user returns 400 Bad Request. Following is the code-snippet
class KeycloakKonfig {
var keycloak: Keycloak = KeycloakBuilder.builder()
.serverUrl("http://localhost:8080/auth")
.username("username")
.password("password")
.realm("realm")
.clientId("clientId")
.clientSecret("clientSecret")
.resteasyClient(ResteasyClientBuilder().connectionPoolSize(20).register(CustomJacksonProvider()).build())
.build()
}
method body in controller
val savedRoleRepresentation = KeycloakKonfig().keycloak.realm("realm").roles().get("role").toRepresentation()
KeycloakKonfig().keycloak.realm("realm").users().get("userId").roles().realmLevel().add(Arrays.asList(savedRoleRepresentation))
Thanks
The problem was the keycloak version. I had Keycloak 4.5.0 which does not allow creating roles and assigning it to user via KeycloakBuilder. I switched to Keycloak 4.8.3 and everything works perfect.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With