Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keycloak Java adapter - Retrieving roles list

I need to retrieve the roles associated to user, but I am working with wildfly, I have installed all jar keycloak in wildfly and my Java project, but can I retrieve this list by Java adapter?

Other options is call the rest api like any api by get, post, put, etc. But my first options was Adapters.

I make the authentication by adapters, but I do not find any way to retrieve roles, clients, realms, etc.

I am wrong or the adapter is just to authentications?

Anyone have a good example?

like image 637
Anonimo142 Avatar asked Feb 07 '18 18:02

Anonimo142


1 Answers

Set the option use-resource-role-mappings : true in keycloak.json and you should be able to get roles in servlet as follows

 KeycloakPrincipal principal = (KeycloakPrincipal)request.getUserPrincipal();

principal.getKeycloakSecurityContext().getToken().getResourceAccess("testclient").getRoles();

You can also get KeycloakPrincipal from context like this

  Subject subject = (Subject) PolicyContext.getContext("javax.security.auth.Subject.container");
  Set<KeycloakPrincipal> principals = subject.getPrincipals(KeycloakPrincipal.class);

and then get the roles

like image 134
ravthiru Avatar answered Oct 23 '22 10:10

ravthiru