Keycloak:在 Java 客户端中创建具有属性的角色

Keycloak: Create role with attributes in Java Client

我正在尝试使用带有一些自定义属性的 keycloak-admin-client (11.0.0) 在 Keycloak(11.0.0) 中创建一个客户端角色。 角色被创建,但是属性字段被 Keycloak 简单地忽略了。有人知道如何让它工作吗?

这是我使用的简化代码:

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);
}

对于此问题的任何帮助,我将不胜感激。谢谢!

致所有遇到同样问题的人: 我刚刚自己找到了解决方法。您需要使用相同的对象更新新创建的角色并且它有效。

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);
    }