以编程方式更改会话超时 [英] Programmatically change session timeout

查看:118
本文介绍了以编程方式更改会话超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在定义的不活动时间后注销用户.

I can logout user after defined time of inactivity.

<session-timeout>240</session-timeout> 

但是,是否有某种方法可以在指定时间或更长时间注销,例如直到在指定时间后5分钟不活动.

But, is there some way to logout in specified time, or better, for example until 5 minutes of inactivity after specified time.?

推荐答案

您可以通过

You can change the session timeout by HttpSession#setMaxInactiveInterval() wherein you can specify the desired timeout in seconds.

当您要满足广泛的要求时,例如文件夹/admin中的所有页面或其他内容,那么执行此操作的最佳位置是创建 Filter 映射到FacesServlet上,它大致完成以下工作:

When you want to cover a broad range of requests for this, e.g. all pages in folder /admin or something, then the best place to do this is to create a Filter which is mapped on the FacesServlet which does roughly the following job:

@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws ServletException, IOException {
    HttpServletRequest request = (HttpServletRequest) req;
    HttpSession session = request.getSession();

    if (request.getRequestURI().startsWith("/admin/")) {
        session.setMaxInactiveInterval(60 * 5); // 5 minutes.
    } else {
        session.setMaxInactiveInterval(60 * 240); // 240 minutes.
    }

    chain.doFilter(req, res);
}

在JSF托管Bean中,ExternalContext#getSession()可以使用该会话:

In a JSF managed bean the session is available by ExternalContext#getSession():

HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession();
// ...

或者,如果您已经在使用JSF 2.1,则还可以使用新的

Or when you're already on JSF 2.1, then you can also use the new ExternalContext#setSessionMaxInactiveInterval() which delegates to exactly that method.

这篇关于以编程方式更改会话超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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