我在 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();
}
}
我不明白为什么缺少更新方法。请帮我。感谢您的建议。
好的,我找到了缺少的方法:
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());
}
}