[英]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);
}
對於這個問題的任何幫助,我將不勝感激。 謝謝!
對於遇到同樣問題的每個人:我自己找到了解決方法。 您需要使用相同的 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.