NullPointerException в sessionContext.getCallerPrincipal()

У меня есть простое (веб-профиль) приложение EJB 3.1, и я пытаюсь определить текущего пользователя в @ApplicationScopedCDI-бине, поэтому я использую:

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

Фасад EJB, который находится между контроллером Faces и службой CDI

@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
задан Ralph 14 May 2012 в 11:04
поделиться