It is injected into the bean - sorry, might not have been enough
code before. A small example:
import javax.annotation.Resource;
import javax.annotation.security.RolesAllowed;
import javax.ejb.SessionContext;
import javax.ejb.Stateless;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.keycloak.KeycloakPrincipal;
import org.keycloak.KeycloakSecurityContext;
import org.keycloak.representations.IDToken;
@Path("/user")
@Stateless
public class UserService {
private static final Log log = LogFactory.getLog(UserService.class);
@Resource
private SessionContext sessionContext;
@Path("/getCurrentUserInfo")
@Produces({ MediaType.APPLICATION_JSON })
@GET
@RolesAllowed({"someRole"})
public Response getCurrentUser() {
@SuppressWarnings("unchecked")
KeycloakPrincipal<KeycloakSecurityContext> kcPrincipal = (KeycloakPrincipal<KeycloakSecurityContext>)(sessionContext.getCallerPrincipal());
IDToken idToken = kcPrincipal.getKeycloakSecurityContext().getIdToken();
log.debug( "email from token is \"" + idToken.getEmail() + "\"" );
// your return is likely something more useful
return Response.ok().build();
}
}
Your use case might be different but this is how it is working for
me. Again, there may be a better way.
On 07/10/2015 05:01 PM, Juan Diego
wrote:
Where do you get sessionContext from?
--
Scott Dunbar
Xigole Systems, Inc.
Enterprise consulting, development, and hosting
303·667·6343