簡體   English   中英

在java中更改KeyCloak用戶屬性的值

[英]Change the value of a KeyCloak user attribute in java

我想在 Keycloak 身份驗證流程中使用用戶屬性作為標志。 如何更改 java 中用戶屬性的值(不使用 api)? 如何在模板(ftl 文件)中引用該屬性 感謝您的指導。

如何在模板(ftl 文件)中引用該屬性

我認為默認情況下應該禁用對用戶屬性的訪問。
解決方案:將用戶屬性設置為表單屬性。 表格代碼:

@Override
protected Response createLoginForm(LoginFormsProvider form) {
    form.setAttribute("customattr", user.getFirstAttribute("customattr"));
    return form.createForm("your-page.ftl");
}

在 ftl 中:

<label>${customattr}</label>

像這樣向 Keycloak-User 添加或更新單個屬性:

    @Transactional
    public void setAttributeByAuthentication(String attributeName, String attributeValue, Authentication auth) {

        // Get realm.
        RealmResource realmResource = this.getRealmResource();

        // Get Keycloak user based on current authentication.
        UserResource userResource = realmResource.users().get(this.getAccessTokenByAuthentication(auth).getSubject());
        UserRepresentation user = userResource.toRepresentation();
        user.singleAttribute(attributeName, attributeValue);
        userResource.update(user);
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM