I found a code, which should reach the logged user:
InitialContext ic = new InitialContext();
SessionContext sessionContext = (SessionContext)ic.lookup("java:comp/EJBContext");
System.out.println("look up injected sctx: " + sessionContext);
Principal p = sessionContext.getCallerPrincipal();
System.out.println(p.getName());
But it still gives me <anonymus>. Why is that? How could I reach the logged user's name?
Your bean needs to be marked as secured (using any of the spec provided ways for the security related interceptors to play a role).
As a first step, you could do something like:
That's just an example where you allow all roles to access that method. The presence of that @PermitAll security annotation will instruct the EJB container to bring into picture the EJB security interceptors. Take a look at this documentation for further details .Giving you jboss example https://docs.jboss.org/author/display/AS72/Securing+EJBs