英文:
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<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!
答案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<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);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论