Keycloak 管理客户端用户更新缺失方法

问题描述 投票:0回答:1

我在 Spring-Boot 项目中使用“org.keycloak:keycloak-admin-client:26.0.2”,我想更新某个领域的用户属性。

import org.keycloak.admin.client.resource.UsersResource;
import org.keycloak.representations.idm.UserRepresentation;
import org.keycloak.admin.client.Keycloak;

...

        Keycloak keycloak = Keycloak.getInstance(
                "https://my-server.org:9443",
                "master",
                cliUser,
                cliPassword,
                "admin-cli");

        UsersResource userResource = keycloak.realm("my-realm").users();
        List<UserRepresentation> foundUsers = userResource.searchByEmail("[email protected]", true);
        Optional<UserRepresentation> user = foundUsers.stream().findFirst();
        if (user.isPresent()) {
            Map<String, List<String>> attribs = user.get().getAttributes();
            attribs.get("language").set(0,"en");
            user.get().setAttributes(attribs);
            try {
                userResource.update(user) //No method with name update
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

我不明白为什么缺少更新方法。请帮我。感谢您的建议。

spring-boot keycloak keycloak-admin-client
1个回答
0
投票

好的,我找到了缺少的方法:

        Optional<UserRepresentation> user = foundUsers.stream().findFirst();
        if (user.isPresent()) {
            Map<String, List<String>> attribs = user.get().getAttributes();
            if (attribs.containsKey("language")) {
                attribs.get("language").set(0,"hu");
                userResource.get(user.get().getId()).update(user.get());
            }
        }
© www.soinside.com 2019 - 2024. All rights reserved.