我有一个Stateful Session Bean
来保存我的登录会话,JSF Session Bean
和Servlet Filter
。
我要做的是阻止非登录用户访问我的网页,所以我做了一个过滤器。
doFilter()
就像这样:
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
String path = req.getRequestURI().substring(req.getContextPath().length());
System.out.println(userManager.isLogged());
if (userManager.isLogged() || path.equals("/") || path.equals("/index.xhtml") || path.startsWith(ResourceHandler.RESOURCE_IDENTIFIER) || path.startsWith("/resources/") || path.startsWith("/admin") || path.equals("/admin/login.xhtml")) {
chain.doFilter(request, response);
} else {
request.getRequestDispatcher("/error.xhtml").forward(request, response);
}
}
其中userManager
位于:
private UserManagerLocal lookupUserManagerLocal() {
try {
Context c = new InitialContext();
return (UserManagerLocal) c.lookup("java:global/UNILIFE/UNILIFE-ejb/UserManager!ejb.UserManagerLocal");
} catch (NamingException ne) {
Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
throw new RuntimeException(ne);
}
}
现在,System.out.println(userManager.isLogged());
打印始终为false,而#{loginBean.logged}
打印为true。
请注意loginBean.logged
只是
public boolean isLogged() {
return userManager.isLogged();
}
并且,在我的Managed Bean中,使用
检索userManager
@EJB
private UserManagerLocal userManager;
似乎servlet不使用与JSF Managed Bean相同的SFSB。
我做错了什么?
编辑:新代码
的servlet
UserManagerLocal userManager = lookupUserManagerLocal();
private UserManagerLocal lookupUserManagerLocal() {
try {
Context c = new InitialContext();
UserManagerLocal userM = (UserManagerLocal) c.lookup("java:global/UNILIFE/UNILIFE-ejb/UserManager!ejb.UserManagerLocal");
HttpServletRequest req = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
req.setAttribute("userManager", userM);
return userM;
} catch (NamingException ne) {
Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
throw new RuntimeException(ne);
}
}
jsf bean
@PostConstruct
public void init(){
HttpServletRequest req = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
userManager = (UserManagerLocal) req.getSession().getAttribute("userManager");
}
答案 0 :(得分:1)
每次查找有状态会话bean时,都会创建一个唯一的实例,因此isLogged可能会返回默认的字段值。您需要以某种方式将有状态会话Bean实例存储在HttpSession中并从过滤器中检索它。我缺乏JSF专业知识,所以我不知道是否有一种方便的方式来共享有状态会话bean实例,或者你是否需要手动将JSF bean链接到有状态bean。
答案 1 :(得分:0)
每个JNDI查找都将返回bean的新句柄。因此,您在调用方法时获取默认值,放弃以前的活动。您需要保存该引用(HttpSession)以进行进一步操作,如其他帖子中所述。
下面是访问JSF bean中会话的示例代码。
HttpServletRequest req = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
根据要求,您可以获得会话和&然后放入其中。
session.setAttribute("userManager", userManager);
现在在过滤器中,您可以从请求中检索bean。另外,相应地修改代码。