the built-in Principal bean is
@Dependentin Weld, that's why
ctx.get(bean)from your sample returns null. Also note that the injected reference is not a "normal" client proxy, but an "EE component proxy". So even if you get the context object for
get()you'll get the proxy. AFAIK there is no way to obtain the instance behind an "EE component proxy". The only thing you could probably do is to register a custom bean for your principal, i.e. with some special qualifier. This bean's
create()method could delegate to the Weld specific
org.jboss.weld.security.spi.SecurityServices.getPrincipal()or directly to some WildFly internals.
First of all - thank you for your time.
I had a feeling this could be a problem :-)
The only way we have found, is to access the service registry from the bean manager, then get the SecurityService and then call getPrincipal() - just like you describe.
This does however become very JBoss / Weld specific. I don't know what the SecurityService does.
Would the same approach work inside an EJB, for example?
SecurityServices is a Weld SPI that allows integrators (WildFly in this case) to provide the Principal representing the current caller identity. FYI there is already a spec issue for this problem: CDI-597. Feel free to add comments.