![](/img/trans.png)
[英]How can I get a user's first, last name, and attribute from keycloak with the Java API?
[英]How to set Custom User Property in keycloak from java api
我在spring应用程序中使用keycloak进行身份验证,如何为用户设置一些属性。 我已经在管理控制台上添加了自定义映射器。
请在所附的屏幕截图下方看一下,在用户部分,有一个可以设置属性的选项卡
现在的问题是如何通过代码访问那些用户属性?
因此,这里是可用于访问用户属性的代码。
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
, Second_User_Attribute
是您在密钥斗篷的用户属性部分中声明的密钥。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.