[keycloak-user] spring security getting username

Jeremy Simon jeremy at jeremysimon.com
Tue Feb 9 14:47:31 EST 2016


Hi,

I can't seem to get my user's name using the keycloak adaptor for
spring security.  I have a rest controller that i'm trying this chunk
of code:

...
Authentication KeyCloakAuth =
SecurityContextHolder.getContext().getAuthentication();
KeycloakAccount  keyAccount = ((KeycloakAuthenticationToken)
KeyCloakAuth).getAccount();

String username1 = keyAccount.getPrincipal().getName()
String username2 =
SecurityContextHolder.getContext().getAuthentication().getName()

KeycloakPrincipal<?> prince = (KeycloakPrincipal<?>)
((KeycloakAuthenticationToken) KeyCloakAuth).getPrincipal();
String username3 = prince.getName();

...


username1, username2, username3 will have something like this:
aa5f6e42-9463-4862-a750-bd0c092daf11


I gleaned this from some stackoverflow examples that claimed these
approached worked... There something I don't have set right?


jeremy
jeremy at jeremysimon.com
www.JeremySimon.com


More information about the keycloak-user mailing list