Java EE 如何扩展 session lifetime/timeout

How Java EE extend session lifetime/timeout

我有一个 Java EE session,如果空闲,它会在 10 分钟内超时。我知道任何命中服务器的请求都会使它 "not idle" 并且它的 session 将被刷新。

我的问题是,Java EE 容器如何在底层处理这个问题?

1) 是因为它在 HTTP 请求中检测到 cookies (JSESSIONID) 的存在 header?

2) 因为调用 request.getSession()request.getSession(false) 是否有下面的技巧?

找了很多帖子也没找到详细的东西

为什么我需要知道这个,因为我遇到了一个问题。我有一个 10 分钟后超时的应用程序。但是我也在几秒钟后定期进行轮询 ajax 运行。也就是说,我的 session 永远不会 expire/timeout。 ajax 轮询包含 cookies (JSESSIONID),但我不确定这是否是主要原因,因为我确实有 Spring 安全过滤器,它可能调用 request.getSession每个 HTTP 请求。

经过一段时间的搜索,我认为没有绝对的答案,因为这取决于个人的实现。

基于我在 servlet-4-final-spec 上找到的规范:

7.6 Last Accessed Times

The getLastAccessedTime method of the HttpSession interface allows a servlet to determine the last time the session was accessed before the current request. The session is considered to be accessed when a request that is part of the session is first handled by the servlet container.

所以我去找Tomcat implementation:

long getLastAccessedTime()

Returns the last time the client sent a request associated with this session, as the number of milliseconds since midnight January 1, 1970 GMT, and marked by the time the container received the request. Actions that your application takes, such as getting or setting a value associated with the session, do not affect the access time.

还是不太清楚,上面说的是"value associated with the session",没说的是getSession

然后我发现this:

org.apache.catalina.core.StandardHostValve.ACCESS_SESSION

If this is true, every request that is associated with a session will cause the session's last accessed time to be updated regardless of whether or not the request explicitly accesses the session. Else the default value will be false.

因此对于 Tomcat,默认情况下,我们必须显式调用 request.getSession 才能刷新会话,除非我们将此 Tomcat 属性设置为 true。

参考:

1) When session is considered accessed

2) http://tomcat.10.x6.nabble.com/Session-timeouts-ignore-quot-periodic-polling-quot-URL-td2159572.html