[英]Assign role to user Keycloak Admin Client
I'm having a problem trying to assign role to user via keycloak admin client.我在尝试通过 keycloak 管理客户端为用户分配角色时遇到问题。 It always returns bad request.它总是返回错误的请求。 First I created a new realm in keycloak with new client.首先,我使用新客户端在 keycloak 中创建了一个新领域。 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.我在 KeycloakBuilder 中使用了该用户的凭据。
Now admin client can create users, but assigning role to user returns 400 Bad Request.现在管理员客户端可以创建用户,但是为用户分配角色会返回 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.问题是keycloak版本。 I had Keycloak 4.5.0 which does not allow creating roles and assigning it to user via KeycloakBuilder.我有 Keycloak 4.5.0,它不允许创建角色并通过 KeycloakBuilder 将其分配给用户。 I switched to Keycloak 4.8.3 and everything works perfect.我切换到 Keycloak 4.8.3,一切正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.