我有一个简单的(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