0

I am using keycloak for authentication in my spring application, how can I set some attribute for a user. I have already added a custom mapper on admin console.

Mr_Hmp
  • 2,474
  • 2
  • 35
  • 53

1 Answers1

0

Please have a look below attached screen shot,in user section there is the tab where you can set the attributes

enter image description here

Now question will be how you will access those user attributes through code?

So here is the code which can be use to access user attributes .

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();
}

Note - First_User_Attribute,Second_User_Attribute are the key you declare in the keycloak's user attribute section.

Subodh Joshi
  • 12,717
  • 29
  • 108
  • 202