java sessioncontext_java – sessionContext.getCallerPrincipal()中的NullPointerException

我有一个简单的(webprofile)EJB 3.1应用程序,并尝试确定@ApplicationScoped CDI Bean中的当前用户,所以我使用:

Principal callerPrincipal = this.sessionContext.getCallerPrincipal()

工作正常(所以我可以确定当前用户的名称).

但是在任何(其他)EJB中的任何异常之后,此调用不再起作用(我需要重新启动服务器)!该方法不会返回调用者主体,而是抛出此异常.

Caused by: java.lang.NullPointerException

at com.sun.ejb.containers.EJBContextImpl.getCallerPrincipal(EJBContextImpl.java:421)

at de.mytest.service.CurrentUserService.getCurrentUserId(CurrentUserService.java:102)

有人能给我一个提示我做错了吗?

实施细节:

Server Glassfish 3.1.2

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);

}

面向Faces Controller和CDI服务之间的EJB Facad

@Stateless

@RolesAllowed("myUser")

public class TeilnehmerServiceEjb {

@Inject

private CurrentUserService currentUserService;

public long currentUserId() {

return = currentUserService.getCurrentUserId();

}

}

web.xml中

All Pages

/*

myUser

BASIC

mySecurityRealm

与GlassFish的web.xml

myUser

APP.MY.USER

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值