首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NullPointerException in sessionContext.getCallerPrincipal()

NullPointerException in sessionContext.getCallerPrincipal()
EN

Stack Overflow用户
提问于 2012-05-14 10:51:32
回答 1查看 7K关注 0票数 7

我有一个简单的(webprofile) EJB3.1应用程序,并尝试在@ApplicationScoped CDI中确定当前用户,因此我使用:

代码语言:javascript
复制
Principal callerPrincipal = this.sessionContext.getCallerPrincipal()

这很好(这样我就可以确定当前用户的名称)。

但是在任何(其他) EJB中出现异常之后,这个调用就不再工作了(我需要重新启动服务器)!方法将抛出此异常,而不是返回调用方主体。

代码语言:javascript
复制
Caused by: java.lang.NullPointerException
at com.sun.ejb.containers.EJBContextImpl.getCallerPrincipal(EJBContextImpl.java:421)
at de.mytest.service.CurrentUserService.getCurrentUserId(CurrentUserService.java:102)

,有人能告诉我我做错了什么吗?

实施细节:

服务器Glassfish 3.1.2

CurrentUserService:

代码语言:javascript
复制
@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);        
    }

在Faces控制器和CDI服务之间抵抗的EJB接口

代码语言:javascript
复制
@Stateless
@RolesAllowed("myUser")
public class TeilnehmerServiceEjb {

    @Inject
    private CurrentUserService currentUserService;

    public long currentUserId() {
        return  = currentUserService.getCurrentUserId();
    }
}

web.xml

代码语言:javascript
复制
<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

代码语言:javascript
复制
<security-role-mapping>
    <role-name>myUser</role-name>
    <group-name>APP.MY.USER</group-name>
</security-role-mapping>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-05-17 13:41:12

它不能工作的原因是因为您的SessionContext对象被声明为一个全局变量,而且由于您使用的是@ApplicationScope,所以在构建应用程序时,通过IoC对该资源的编程将只进行一次。

如果您想将bean保持为@ApplicationScope,我建议您每次在执行该操作的方法中手动访问SessionContext时尝试访问它,但是使用JNDI代替IoC。请参见示例,该示例如何使用以下方法查看热执行JNDI查找以手动访问资源:

代码语言:javascript
复制
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);
      }
//..

}

如果您对访问SessionContext的更多方法感兴趣,请查看下面的链接,我在其中发现了代码片段:

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

我希望这能帮到你

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10581921

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档