我希望在我所有jsf页面的标题中应用相同的代码,它会自动检测会话何时超时并重定向到登录页面。
我已尝试过某些在线教程的代码但未能做到这一点。
有人可以教我吗?
答案 0 :(得分:1)
这不是很好的方法。使用简单的登录或会话到期过滤器:
public class LoginFilter implements Filter {
/**
* Checks if user is logged in. If not it redirects to the login.xhtml page.
*/
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
// Get the loginBean from session attribute
LoginBean loginBean = (LoginBean)((HttpServletRequest)request).getSession().getAttribute("loginBean");
// For the first application request there is no loginBean in the session so user needs to log in
// For other requests loginBean is present but we need to check if user has logged in successfully
if (loginBean == null || !loginBean.isLoggedIn()) {
String contextPath = ((HttpServletRequest)request).getContextPath();
((HttpServletResponse)response).sendRedirect(contextPath + "/login.xhtml");
}
chain.doFilter(request, response);
}
public void init(FilterConfig config) throws ServletException {
// Nothing to do here!
}
public void destroy() {
// Nothing to do here!
}
}
的web.xml
<!-- Login filter -->
<filter>
<filter-name>LoginFilter</filter-name>
<filter-class>com.examples.webapps.filterlogin.filters.LoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>LoginFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
答案 1 :(得分:1)
在服务器端,您的代码可以了解会话超时。使用它,您可以设置一个cookie,指示实际会话超时发生的时间(未提及活动会话超时或空闲超时)。页面上运行的javascript可以定时读取cookie,并在超过超时值时重定向用户。
这是众多方法中的一种,希望它有所帮助。
我认为过滤器不是必需的方法,因为要求是指自动重定向到登录屏幕。