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

huangapple go评论84阅读模式
英文:

Keycloak: Create role with attributes in Java Client

问题

我正在尝试在Keycloak(11.0.0)中使用keycloak-admin-client(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);
}

非常感谢您对此问题的任何帮助。谢谢!

英文:

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.
The role gets created, but the attribute field is simply ignored by 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&lt;String, List&lt;String&gt;&gt; attributes = new HashMap&lt;&gt;();
    attributes.put(&quot;att1&quot;, Collections.singletonList(&quot;attribute1&quot;));
    attributes.put(&quot;att2&quot;, Collections.singletonList(&quot;attribute2&quot;));
    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!

答案1

得分: 1

对于所有正在与相同问题挣扎的人:
我刚刚找到了一个解决方法。您需要使用相同的对象更新新创建的角色,然后它就会起作用。

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);
        
        // 现在立即更新新角色
        RoleResource roleResource = realm.clients().get(client.getId()).roles().get(name);
        roleResource.update(roleRepresentation);
    }
英文:

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.

public void createRole(String name) {
        RoleRepresentation roleRepresentation = new RoleRepresentation();
        Map&lt;String, List&lt;String&gt;&gt; attributes = new HashMap&lt;&gt;();
        attributes.put(&quot;att1&quot;, Collections.singletonList(&quot;attribute1&quot;));
        attributes.put(&quot;att2&quot;, Collections.singletonList(&quot;attribute2&quot;));
        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);
    }

huangapple
  • 本文由 发表于 2020年8月19日 17:37:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/63484102.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定