简体   繁体   English

Keycloak:在Java Client中创建具有属性的角色

[英]Keycloak: Create role with attributes in Java Client

I am trying to create a client role in Keycloak(11.0.0) with keycloak-admin-client (11.0.0) with a few custom attributes.我正在尝试使用带有一些自定义属性的 keycloak-admin-client (11.0.0) 在 Keycloak(11.0.0) 中创建一个客户端角色。 The role gets created, but the attribute field is simply ignored by Keycloak.角色被创建,但是属性字段被 Keycloak 简单地忽略了。 Has anybody an idea how to get it working?有没有人知道如何让它工作?

This is the simplified code I am using:这是我使用的简化代码:

public void createRole(String name) {
    RoleRepresentation roleRepresentation = new RoleRepresentation();
    Map<String, List<String>> attributes = new HashMap<>();
    attributes.put("att1", Collections.singletonList("attribute1"));
    attributes.put("att2", Collections.singletonList("attribute2"));
    roleRepresentation.setAttributes(attributes);
    roleRepresentation.setClientRole(true);
    roleRepresentation.setName(name);
    realm.clients().get(client.getId()).roles().create(roleRepresentation);
}

I would greatly appreciate any help with this issue.对于这个问题的任何帮助,我将不胜感激。 Thanks!谢谢!

For everyone who is struggling with the same problem: I just found a workaround myself.对于遇到同样问题的每个人:我自己找到了解决方法。 You need to update the newly created role with the same object and it works.您需要使用相同的 object 更新新创建的角色并且它可以工作。

public void createRole(String name) {
        RoleRepresentation roleRepresentation = new RoleRepresentation();
        Map<String, List<String>> attributes = new HashMap<>();
        attributes.put("att1", Collections.singletonList("attribute1"));
        attributes.put("att2", Collections.singletonList("attribute2"));
        roleRepresentation.setAttributes(attributes);
        roleRepresentation.setClientRole(true);
        roleRepresentation.setName(name);
        realm.clients().get(client.getId()).roles().create(roleRepresentation);
        
        // Now update the new role immediately
        RoleResource roleResource = realm.clients().get(client.getId()).roles().get(name);
        roleResource.update(roleRepresentation);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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