[keycloak-user] Getting the current user name in EJB

Bill Burke bburke at redhat.com
Fri Oct 31 15:10:16 EDT 2014


You should be able to typecast Principal to KeycloakPrincipal.

On 10/31/2014 2:27 PM, Alexander Chriztopher wrote:
> Hi All,
>
> Am trying to get the name and surname of the currently connected user by
> doing this :
>
> import java.io.Serializable;
> import java.security.Principal;
>
> import javax.annotation.Resource;
> import javax.annotation.security.RolesAllowed;
> import javax.ejb.EJBContext;
> import javax.ejb.LocalBean;
> import javax.ejb.Stateless;
>
> import org.jboss.ejb3.annotation.SecurityDomain;
>
>
> @Stateless(name="myEJB")
> @LocalBean
> @SecurityDomain("keycloak")
> public class MyEJB implements Serializable {
>
>      private static final long serialVersionUID = 1L;
>
>      @Resource
>      private EJBContext ejbContext;
>      @RolesAllowed("ADMIN")
>      public void test() {
>          Principal principal = ejbContext.getCallerPrincipal();
>          System.out.println("principal.getName() = " + principal.getName());
>      }
> }
>
> This works nicely as i get a 403 if my currently connected user does
> have the role : ADMIN.
>
> My question is : does keycloak propagate the username or any other
> information that would help me get the first name and last name of the
> currently connected user ? Unfortunately, principal.getName() returns a
> string like this : edd42240-85bf-4724-8d79-5374338506b7 which i don't
> know the interpretation !
>
> Thanks for any help.
>
>
> _______________________________________________
> keycloak-user mailing list
> keycloak-user at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/keycloak-user
>

-- 
Bill Burke
JBoss, a division of Red Hat
http://bill.burkecentral.com


More information about the keycloak-user mailing list