在我的rest服务中,我可以在身份验证之后获得主体信息,使用
KeycloakPrincipal kcPrincipal = (KeycloakPrincipal) servletRequest.getUserPrincipal();语句。
Keycloak主体没有包含我需要的关于经过身份验证的用户的所有信息。可以自定义我自己的主体类型吗?在keycloak-server端,我开发了一个用户联盟提供者。我看到UserModel使得向我的用户添加一组自定义属性成为可能。
是否可以在代码中插入我自定义主体?
是否可以从keycloak主体检索此属性?
方法是什么?
发布于 2015-10-01 22:22:51
要添加自定义属性,您需要做三件事:
第一个在这里解释得很好:https://www.keycloak.org/docs/latest/server_admin/index.html#user-attributes
添加声明映射:
<
访问声明:
final Principal userPrincipal = httpRequest.getUserPrincipal();
if (userPrincipal instanceof KeycloakPrincipal) {
KeycloakPrincipal<KeycloakSecurityContext> kp = (KeycloakPrincipal<KeycloakSecurityContext>) userPrincipal;
IDToken token = kp.getKeycloakSecurityContext().getIdToken();
Map<String, Object> otherClaims = token.getOtherClaims();
if (otherClaims.containsKey("YOUR_CLAIM_KEY")) {
yourClaim = String.valueOf(otherClaims.get("YOUR_CLAIM_KEY"));
}
} else {
throw new RuntimeException(...);
}希望这对你有帮助,适合你的用例。我将其用于添加自定义主题的自定义属性。
发布于 2017-12-07 02:55:33

映射器选择客户端>单击客户端ID >转到映射器选项卡> create



的自定义属性


更新
的'phone‘属性
https://stackoverflow.com/questions/32678883
复制相似问题