在JSF应用程序中自动注销 [英] Auto-logout in JSF Application

查看:82
本文介绍了在JSF应用程序中自动注销的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JSF应用程序,并希望用户在一段时间不活动后自动注销。是否有标准的方法来执行此操作?

I have a JSF app and would like to have the user auto logout after a period of inactivity. Is there an standard way to do this?

推荐答案

通常,托管网络的服务器(Tomcat,Glassfish ...)应用程序处理会话超时。

Generally, the server (Tomcat, Glassfish...) that hosts the web application handles a timeout for a session.

例如,在Tomcat中,您可以通过在<$ c中添加以下行来定义特定Web应用程序的会话超时$ c> web.xml 文件:

For example, in Tomcat, you can define the session timeout for a particular web application by adding the folowing lines in the web.xml file:

<session-config>
    <session-timeout>30</session-timeout>
</session-config>

这会将超时设置为30分钟。

This will set the timeout to 30 minutes.

如果用户在超过此定义的超时的时间内未发送任何请求,则服务器上的会话将失效。如果用户在会话失效后尝试重新连接,则通常会将其重定向到另一个页面或错误页面。

When a user does not send any request during a time greater that this defined timeout, the session on the server is invalidated. If the user tries to reconnect after the session has been invalidated, he will generally be redirected to another page or to an error page.

您可以开发自己的JSF过滤器将自动将用户重定向到 timeout.html 页面。以下是此类过滤器的示例:

You can develop your own JSF Filter that will automatically redirect the user to a timeout.html page. Here is an example of such a filter :

public class TimeoutFilter implements Filter { 

    private static final String TIMEOUT_PAGE = "timeout.html"; 
    private static final String LOGIN_PAGE = "login.faces";  

    public void init(FilterConfig filterConfig) throws ServletException { 
    } 

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException { 
    if ((request instanceof HttpServletRequest) && (response instanceof HttpServletResponse)) { 
        HttpServletRequest requestHttp = (HttpServletRequest) request; 
        HttpServletResponse responseHttp = (HttpServletResponse) response; 
        if (checkResource(requestHttp)) {
            String requestPath = requestHttp.getRequestURI();
            if (checkSession(requestHttp)) { 
                String timeoutUrl = hRequest.getContextPath() + "/" + TIMEOUT_PAGE; 
                responseHttp.sendRedirect(timeoutUrl); 
                return; 
            } 
        } 
        filterChain.doFilter(request, response);
    } 

    private boolean checkResource(HttpServletRequest request) { 
        String requestPath = request.getRequestURI(); 
        return !(requestPath.contains(TIMEOUT_PAGE) || requestPath.contains(LOGIN_PAGE) || requestPath.equals(hRequest.getContextPath() + "/")); 
    } 

    private boolean checkSession(HttpServletRequest request) { 
        return request.getRequestedSessionId() != null && !request.isRequestedSessionIdValid(); 
    }

    public void destroy() { 
    } 

}

这篇关于在JSF应用程序中自动注销的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆