我在spring应用程序中使用keycloak进行身份验证,如何为用户设置一些属性。 我已经在管理控制台上添加了自定义映射器。
答案 0 :(得分:0)
请在所附的屏幕截图下面查看,在用户部分,您可以在其中设置属性的标签
现在的问题是您将如何通过代码访问那些用户属性?
这是可用于访问用户属性的代码。
@click="setProductCategory(category.id);"
注意-HttpServletRequest httpRequest = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
KeycloakSecurityContext securityContext = (KeycloakSecurityContext) httpRequest.getAttribute(KeycloakSecurityContext.class.getName());
AccessToken accessToken = securityContext.getToken();
if(null != accessToken ){
Map<String, Object> otherClaims = accessToken.getOtherClaims() ;
tgtToken = securityContext.getTokenString();
String firstUserAtt = otherClaims.get("First_User_Attribute").toString();
String secondUserAtt = otherClaims.get("Second_User_Attribute").toString();
}
,First_User_Attribute
是您在密钥斗篷的用户属性部分中声明的密钥。