NullPointerException in sessionContext.getCallerPrincipal ()

I have a simple (webprofile) EJB 3.1 application and try to determine the current user in the @ApplicationScoped CDI Bean, so I use:

 Principal callerPrincipal = this.sessionContext.getCallerPrincipal() 

works fine (so I can determine the name of the current user).

But after any exception in any (other) EJB, this call no longer works (I need to restart the server)! Instead of returning the main call to the caller, the method throws this exception.

 Caused by: java.lang.NullPointerException at com.sun.ejb.containers.EJBContextImpl.getCallerPrincipal(EJBContextImpl.java:421) at de.mytest.service.CurrentUserService.getCurrentUserId(CurrentUserService.java:102) 

Can anyone give me a hint what am I doing wrong?


Implementation Details:

Glassfish 3.1.2 Server

CurrentUserService:

 @ApplicationScoped public class CurrentUserService { @Resource private SessionContext sessionContext; public long getCurrentUserId() { if (this.sessionContext == null) { throw new RuntimeException("initialization error, sessionContext must not be null!"); } /*line 102 */ Principal callerPrincipal = this.sessionContext.getCallerPrincipal(); if (callerPrincipal == null) { throw new RuntimeException("callerPrincipal must not be null, but it is"); } String name = callerPrincipal.getName(); if (name == null) { throw new RuntimeException("could not determine the current user id, because no prinicial in session context"); } return this.getUserIdForLogin(name); } 

EJB Facad Resist Between Faces Controller and CDI Service

 @Stateless @RolesAllowed("myUser") public class TeilnehmerServiceEjb { @Inject private CurrentUserService currentUserService; public long currentUserId() { return = currentUserService.getCurrentUserId(); } } 

web.xml

 <security-constraint> <web-resource-collection> <web-resource-name>All Pages</web-resource-name> <url-pattern>/*</url-pattern> </web-resource-collection> <auth-constraint> <role-name>myUser</role-name> </auth-constraint> </security-constraint> <login-config> <auth-method>BASIC</auth-method> <realm-name>mySecurityRealm</realm-name> </login-config> 

GlassFish-web.xml

 <security-role-mapping> <role-name>myUser</role-name> <group-name>APP.MY.USER</group-name> </security-role-mapping> 
+6
source share
1 answer

The reason this does not work is because your SessionContext is declared as a global variable, and since you are using @ApplicationScope, this resource will only be initialized through IoC once the application is built.

If you want to save the bean as @ApplicationScope, I would recommend that you try to access the SessionContext every time you need it manually, from a method that performs this action, but instead of IoC, use the JNDI API manually. See an example on how to see how hot it is to perform a JNDI search to access a resource manually using:

 public long getCurrentUserId() { //.. try { InitialContext ic = new InitialContext(); SessionContext sessionContext=(SessionContext) ic.lookup("java:comp/env/sessionContext"); System.out.println("look up injected sctx: " + sessionContext); //Now do what you want with the Session context: Principal callerPrincipal = sessionContext.getCallerPrincipal(); //.. } catch (NamingException ex) { throw new IllegalStateException(ex); } //.. } 

If you're interested in learning about other ways to access the SessionContext, look at this link where I found this snipet code:

http://javahowto.blogspot.com/2006/06/4-ways-to-get-ejbcontext-in-ejb-3.html

I hope this helps

+3
source

Source: https://habr.com/ru/post/915654/


All Articles