我一直在寻找有关该主题的许多文章,但找不到适合我的解决方案。
我正在将Java EE 6与JSF 2.0结合使用(在JBoss AS 7.1上部署)
在我的web.xml我有:
1
并且我 希望在会话自动超时时将用户重定向到登录页面。
我尝试过的
方法1:使用过滤器
我尝试了以下过滤器:
@WebFilter()
public class TimeOutFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
ServletException {
System.out.println("filter called");
final HttpServletRequest req = (HttpServletRequest) request;
final HttpSession session = req.getSession(false);
if (session != null && !session.isNew()) {
chain.doFilter(request, response);
} else {
System.out.println("Has timed out");
req.getRequestDispatcher("/logon.xthml").forward(request, response);
}
}
@Override
public void destroy() {
}
}
在web.xml我尝试过的
TimeOutFilter
*.xhtml
和
TimeOutFilter
Faces Servlet
过滤器在每次请求时都会被调用(在控制台中记录“被调用的适配器”)。 但是,会话超时时不会调用它。
方法2:HttpSessionLister
我尝试使用HttpSessionListerner。该方法称为具有以下签名:
public void sessionDestroyed(HttpSessionEvent se) {
}
我无法重定向到特定页面。当我想重定向用户时,通常使用NavigationHandlerfrom中的,FacesContext但是在这种情况下,没有FacesContext(FacesContext.getCurrentInstance()return
null)。
根据这篇文章,HttpListener不能重定向用户,因为它不是请求的一部分。
题
解决此问题的最佳方法是什么?如何使上述两种方法之一起作用?