简体   繁体   中英

Assign role to user Keycloak Admin Client

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM