我正在尝试使用具有一些自定义属性的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);
}
对于此问题的任何帮助,我将不胜感激。谢谢!
答案 0 :(得分: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);
// Now update the new role immediately
RoleResource roleResource = realm.clients().get(client.getId()).roles().get(name);
roleResource.update(roleRepresentation);
}