Tho*_*lan 10 resteasy openid-connect keycloak-services
我已经从 Keycloak 的 OIDC 端点中提取了用户的组信息,但它们没有随我定义的组 ATTRIBUTES 一起提供(请参阅组表单中的“属性”选项卡,靠近“设置”)。是否有权利要求添加到我的请求中?
我正在使用 RESTeasy 客户端访问 Keycloak 的管理 API(结果比使用提供的管理客户端好得多):
@Path("/admin/realms/{realm}")
public interface KeycloakAdminService {
@GET
@Path("/users/{id}/groups")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
List<GroupRepresentation> getUserGroups(@PathParam("realm") String realm, @PathParam("id") String userId,
@HeaderParam(AUTHORIZATION) String accessToken);
//DEBUG the access token must always be prefixed by "Bearer "
}
Run Code Online (Sandbox Code Playgroud)
所以我可以获取用户的组:
private void fetchUserGroups(UserInfoOIDC infos, String userId) {
log.info("Fetching user groups from {}...", getRealm());
try {
KeycloakAdminService proxy = kcTarget.proxy(KeycloakAdminService.class);
AccessTokenResponse response = authzClient.obtainAccessToken(getAdminUsername(), getAdminPassword());
List<GroupRepresentation> groups = proxy.getUserGroups(getRealm(), userId,
"Bearer " + response.getToken());
infos.importUserGroups(groups); //DEBUG here we go!
} catch (WebApplicationException e) {
log.error("User groups failure on {}: {}", getRealm(), e.getMessage());
}
}
Run Code Online (Sandbox Code Playgroud)
但是当涉及到数据探索时,事实证明 GroupRepresentation#getAttributes 结构中没有提供任何属性。
我读过可以将声明添加到用户信息请求中。它适用于管理 API 吗?如何使用 RESTeasy 模板实现该结果?谢谢
try*_*arn 26
我能够通过在令牌其他声明属性中添加组/角色信息来实现这一点:
为此,在 keycloak 配置中,转到您的客户端 -> 映射器并添加组/角色映射器。例如
现在此信息将开始出现在您的访问令牌中:
要在 Java 中访问这些组属性,您可以从otherclaimsaccesstoken 的属性中提取它。例如:
KeycloakSecurityContext keycloakSecurityContext = (KeycloakSecurityContext)(request.getAttribute(KeycloakSecurityContext.class.getName()));
AccesToken token = keycloakSecurityContext.getToken();
Run Code Online (Sandbox Code Playgroud)
在下图中,您可以看到otherclaims令牌的属性填充了我们在 keycloak 上创建的组属性。请注意,如果我们将“令牌声明属性”命名为 groupXYZ,otherclaims则将显示:
groupsXYZ=[Administrator]
这就是我最终如何将组属性(如之前怀疑的那样继承为用户属性)映射到用户信息,到“其他声明”部分:
| 归档时间: |
|
| 查看次数: |
16791 次 |
| 最近记录: |