I have secured an enterprise application with Keycloak using standard wildfly based Keycloak adapters. Issue that I am facing is that the rest web services when invoked, needs to know the username that is currently logged in. How do I get the logged in user information from Keycloak?
I tried using SecurityContext
, WebListener
etc. But none of them are able to give me the required details.
Need to add standalone.xml next line:
Example:
In my case i was taking the preferred user name from the token like this
To work i had to go to keycloak and add on my client template the add builtins if not added preferred username came null.
Check the username on the built ins, client template -> mappers.
After that if worked!
You may also set the
principal-attribute
property in thekeycloak.json
file of your web app topreferred_username
.In Keycloak 3.4.3 (may also work on earlier versions) I was able to map username to the
sub
token claim name. From the Keycloak admin interface this is done underClients > [your-client] > Mappers > username
and then entersub
in theToken Claim Name
field. This has the advantage of actually changing the contents of theID token
returned by Keycloak rather than adjusting client-side as in the other answer. This is particularly nice when you're using a standard OpenID Connect library rather than an adapter provided by Keycloak.You get all user information from the security context.
Example:
For the security context to be propagated you have to have a security domain configured as described in the: JBoss/Wildfly Adapter configuration