如何在 Tomcat 中配置应用程序会话的最长持续时间?

How to configure a maximum duration of an application session in Tomcat?

我需要将应用程序会话的最长持续时间配置为 Tomcat 到 24 小时。

我无法在文档中找到合适的配置:

https://tomcat.apache.org/tomcat-8.5-doc/config/http.html

( SSLHostConfigsessionTimeout 但我需要 Connector 配置;我们在 Tomcat 之前终止了 WebServer 中的 SSL 连接,但会话管理由Tomcat.)

已添加

我们已经处理了会话过期超时 (Tomcat Session Timeout web.xml)。

最大持续时间超时意味着即使用户在所有时间都处于活动状态,其应用程序会话也会在最大持续时间超时后失效。

可以实施 HttpSessionListener 并在 24 小时后销毁会话:

https://tomcat.apache.org/tomcat-8.5-doc/servletapi/javax/servlet/http/HttpSessionListener.html

问题是否存在更好的方法。

您可以使用 setMaxInactiveInterval

配置最大持续时间会话

Specifies the time, in seconds, between client requests before the servlet container will invalidate this session.

创建会话时更新,使用 覆盖 sessionCreated 方法:

public class MyHttpSessionListener implements HttpSessionListener{
  public void sessionCreated(HttpSessionEvent event){
    event.getSession().setMaxInactiveInterval(24*60*60); //24 Hours
  }

use a HttpSessionListener. In the sessionCreated() method, you can set the session timeout programmatically.

HttpSessionListener 只会通知 session 创建和销毁,但不会在每个页面请求时调用。

我会实施一个过滤器来检查 session 创建时间并使 session 加上设置 headers 或重定向无效。

在web.xml中添加:

<filter>
    <filter-name>Max Session Duration</filter-name>
    <filter-class>com.your.package.MaxSessionDurationFilter</filter-class>
    <init-param>
        <!-- Maximum session duration in hours -->
        <param-name>maxduration</param-name>
        <param-value>24</param-value>
    </init-param>
</filter>

和像

这样的映射
<filter-mapping>
  <filter-name>Max Session Duration</filter-name>
  <url-pattern>*.jsp</url-pattern>
</filter-mapping>

然后过滤器实现如下:

package com.your.package;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MaxSessionDurationFilter implements Filter {

    private final long oneHourMillis = 1000*60*60;

    private long maxDuration;

    private FilterConfig filterConfig;

    @Override
    public void init(FilterConfig fc) throws ServletException {
        filterConfig = fc;
        maxDuration = Long.parseLong(filterConfig.getInitParameter("maxduration"));
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
        throws IOException, ServletException {
        HttpServletRequest httpReq = (HttpServletRequest) req;
        HttpServletResponse httpResp = (HttpServletResponse) resp;
        final long creationTime = httpReq.getSession().getCreationTime();
        final long currentTime = System.currentTimeMillis();
        if (currentTime-creationTime > maxDuration*oneHourMillis) {
            httpReq.getSession().invalidate();
            // Could also set headers to 403 forbidden
            // httpResp.setStatus(HttpServletResponse.SC_FORBIDDEN);
            httpResp.sendRedirect("expiredsession.jsp");
        } else {
            chain.doFilter(req, resp);
        }
    }

    @Override
    public void destroy() { }

}