Trouble getting principal name in EJB
tbronzan Feb 19, 2014 4:21 PMI'm having problems with the session context inside an EJB not being able to find the principal name under certain circumstances.
Here's my EJB
@Stateless @SecurityDomain("MyDomain") public class UserCookieEJB implements IUserCookieRetrieverLocal, IUserCookieRetrieverRemote, IUserCookieUpdaterLocal, IUserCookieUpdaterRemote { @Resource private SessionContext context; protected String getPrincipalName() //helper method used in interface public methods { return context.getCallerPrincipal().getName(); } ... }
I have two web projects and a common project that they both use classes from, all of which are packaged in an ear. If I do a lookup for the UserCookieEJB using a class from one of the web projects then the call to getPrincipalName() returns the correct principal. However, if the UserCookieEJB is looked up from one of the classes in the common project, getPrincpalName() returns anonymous. The ejb subsystem has <default-security-domain value="MyDomain"/> set. The classes in the web projects and common project use a helper class to do the lookup, so they lookups are done the same way and the helper class is located in the common project.
EDIT: It would seem that there is a problem with the SessionContext in general as I'm seeing this in another place in the application as well. Sometimes the SessionContext has the right principal, other times it does not.
EDIT 2: Ok it looks like it's a problem with the principal not being propagated properly, and may be similar to this: Principal propagation from web to ejb
Here is the output I get when I check the principal name in the two EJBs we have the use the @Resource SessionContext. It looks like it only gets the correct principal name from one thread.
16:18:02,378 ERROR [stderr] (default task-4) SessionInfoEJB: superuser 16:18:02,410 ERROR [stderr] (default task-4) SessionInfoEJB: superuser 16:18:02,440 ERROR [stderr] (default task-4) SessionInfoEJB: superuser 16:18:02,477 ERROR [stderr] (default task-4) SessionInfoEJB: superuser 16:18:02,499 ERROR [stderr] (default task-4) SessionInfoEJB: superuser 16:18:02,520 ERROR [stderr] (default task-4) SessionInfoEJB: superuser 16:18:02,538 ERROR [stderr] (default task-4) UserCookieEJB: superuser 16:18:02,549 ERROR [stderr] (default task-4) SessionInfoEJB: superuser 16:18:02,650 ERROR [stderr] (Timer-5) SessionInfoEJB: anonymous 16:18:31,789 ERROR [stderr] (Timer-7) SessionInfoEJB: anonymous 16:18:31,794 ERROR [stderr] (Timer-7) SessionInfoEJB: anonymous 16:18:31,805 ERROR [stderr] (Timer-7) SessionInfoEJB: anonymous 16:18:31,818 ERROR [stderr] (Timer-7) SessionInfoEJB: anonymous 16:18:31,829 ERROR [stderr] (Timer-7) SessionInfoEJB: anonymous 16:18:31,840 ERROR [stderr] (Timer-7) SessionInfoEJB: anonymous 16:18:31,851 ERROR [stderr] (Timer-7) SessionInfoEJB: anonymous 16:18:31,863 ERROR [stderr] (Timer-7) SessionInfoEJB: anonymous 16:18:31,875 ERROR [stderr] (Timer-7) SessionInfoEJB: anonymous 16:18:31,886 ERROR [stderr] (Timer-7) SessionInfoEJB: anonymous 16:18:31,898 ERROR [stderr] (Timer-7) SessionInfoEJB: anonymous 16:18:31,909 ERROR [stderr] (Timer-7) SessionInfoEJB: anonymous 16:18:31,941 ERROR [stderr] (Timer-7) SessionInfoEJB: anonymous 16:18:31,946 ERROR [stderr] (Timer-7) SessionInfoEJB: anonymous 16:19:47,924 ERROR [stderr] (Timer-10) UserCookieEJB: anonymous 16:19:47,926 ERROR [stderr] (Timer-10) UserCookieEJB: anonymous