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

Alexander Chriztopher alexander.chriztopher at gmail.com
Fri Oct 31 14:27:12 EDT 2014


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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/keycloak-user/attachments/20141031/45eb5c5e/attachment.html 


More information about the keycloak-user mailing list