Authentication KeyClope检索KeyClope主体的自定义属性
在我的rest服务中,我可以使用Authentication KeyClope检索KeyClope主体的自定义属性,authentication,jboss,customization,principal,keycloak,Authentication,Jboss,Customization,Principal,Keycloak,在我的rest服务中,我可以使用 KeycloakPrincipal kcPrincipal = (KeycloakPrincipal) servletRequest.getUserPrincipal(); 声明 Keyclope principal不包含我需要的关于经过身份验证的用户的所有信息。 是否可以自定义我自己的主体类型? 在KeyClope服务器端,我开发了一个用户联合提供者。我看到UserModel可以向我的用户添加一组自定义属性 可以在代码中插入我的自定义主体吗 是否可以从Key
KeycloakPrincipal kcPrincipal = (KeycloakPrincipal) servletRequest.getUserPrincipal();
声明
Keyclope principal不包含我需要的关于经过身份验证的用户的所有信息。
是否可以自定义我自己的主体类型?
在KeyClope服务器端,我开发了一个用户联合提供者。我看到UserModel可以向我的用户添加一组自定义属性
可以在代码中插入我的自定义主体吗
是否可以从KeyClope主体检索此属性
方法是什么?要添加自定义属性,您需要做三件事:
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(...);
}
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(...);
}
final-Principal-userPrincipal=httpRequest.getUserPrincipal();
if(userPrincipal instanceof keydropecipal){
keydepaprincipal kp=(keydeprincipal)userPrincipal;
IDToken token=kp.getKeyDopperSecurityContext().getIdToken();
Map otherClaims=token.getOtherClaims();
如果(otherClaims.containsKey(“您的索赔密钥”)){
yourClaim=String.valueOf(otherClaims.get(“YOUR_CLAIM_KEY”);
}
}否则{
抛出新的RuntimeException(…);
}
希望这有助于并适合您的用例。我将其用于添加自定义主题的自定义属性。- 选择用户>查找>单击ID>转到属性选项卡>添加属性>例如:电话>保存
- 选择客户端>单击客户端ID>转到映射器选项卡>创建映射器
- 获取自定义属性
- 在组级别上添加“phone”属性,将用户分配到该组,然后从组级别为所有用户获取“phone”属性
- 返回mapper并使用“聚合属性值=true”和“多值=true”更新“phone”,您将获得“phone”作为列表,其中包含来自组和用户级别的属性。如果保留“聚合属性值=false”或“多值=false”,则只会得到一个值,其中来自用户的“phone”属性将覆盖来自组的“phone”属性(这很有意义)