如何通过Java API在Keycloak中设置自定义用户属性

时间:2018-07-02 10:19:39

标签: java authentication spring-security keycloak keycloak-services

我在spring应用程序中使用keycloak进行身份验证,如何为用户设置一些属性。 我已经在管理控制台上添加了自定义映射器。

1 个答案:

答案 0 :(得分:0)

请在所附的屏幕截图下面查看,在用户部分,您可以在其中设置属性的标签

enter image description here

现在的问题是您将如何通过代码访问那些用户属性?

这是可用于访问用户属性的代码。

@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是您在密钥斗篷的用户属性部分中声明的密钥。