简体   繁体   English

为用户 Keycloak Admin Client 分配角色

[英]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.

相关问题 无法使用keycloak-admin-client在Keycloak中创建用户 - Cannot create user in Keycloak with keycloak-admin-client 尝试使用 spring rest 客户端创建 Keycloak 用户 - Trying to create a Keycloak user with spring rest client 无法使用 keycloak admin cli 端点接收客户端密钥 - Unable to receive Client Secret using keycloak admin cli endpoint Keycloak 角色映射 - Keycloak role mapping Keycloak/OpenID:代表客户端应用程序请求用户信息 - Keycloak/OpenID: request user-information on behalf of the client application 无法为 Hibernate 分配默认的 user_role="ROLE_USER" - Can't assign a default user_role="ROLE_USER" with Hibernate Spring 启动微服务 Keycloak javax.ws.rs.NotFoundException: HTTP 404 未找到(无法创建具有角色的用户) - Spring Boot Microservice Keycloak javax.ws.rs.NotFoundException: HTTP 404 Not Found (Cannot Create User with Role) Keycloak admin-client:无法找到内容类型application / json的MessageBodyReader - Keycloak admin-client: Unable to find a MessageBodyReader of content-type application/json @Secured({“ ROLE_USER”,“ ROLE_ADMIN”})的确切含义 - What does @Secured({ “ROLE_USER”, “ROLE_ADMIN” }) exactly means Spring Security-为什么我只能以ROLE_ADMIN而不是ROLE_USER的身份访问页面? - Spring security - how come I can only access pages as ROLE_ADMIN and not ROLE_USER?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM