У меня есть простой (webprofile) EJB 3.1 приложения и попытаться сделать определение текущего пользователя в пределах @ApplicationScoped
КДИ Bean, так что я использую:NullPointerException в sessionContext.getCallerPrincipal()
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)
ли кто-нибудь может дать мне намек, что я делаю не так?
Детали реализации:
сервера 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-Facad, которые сопротивляются между контроллером Лики и КДИ службы
@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>
Почему бы не сделать это SessionScoped? –
Это похоже на дефект Glassfish. –
Если вы создаете приложение, которое просто делает то, что вы описываете, вы можете воспроизвести поведение? Если это так, я бы посоветовал вам указать ошибку. – Preston