我有一个简单的(webprofile) EJB3.1应用程序,并尝试在@ApplicationScoped CDI中确定当前用户,因此我使用:
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);
}在Faces控制器和CDI服务之间抵抗的EJB接口
@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>发布于 2012-05-17 13:41:12
它不能工作的原因是因为您的SessionContext对象被声明为一个全局变量,而且由于您使用的是@ApplicationScope,所以在构建应用程序时,通过IoC对该资源的编程将只进行一次。
如果您想将bean保持为@ApplicationScope,我建议您每次在执行该操作的方法中手动访问SessionContext时尝试访问它,但是使用JNDI代替IoC。请参见示例,该示例如何使用以下方法查看热执行JNDI查找以手动访问资源:
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
我希望这能帮到你
https://stackoverflow.com/questions/10581921
复制相似问题