web.xml 中单个会话配置元素中的多个跟踪模式元素?

Multiple tracking-mode elements within a single session-config element in the web.xml?

我正在为我的 Web 应用程序使用 Tomcat 8.0.35。这个post

https://www.logicbig.com/tutorials/java-ee-tutorial/java-servlet/session-tracking-mode.html

表示我们可以在 web.xml

的单个会话配置元素中使用多个跟踪模式元素
<web-app>
    <session-config>
        <tracking-mode>???</tracking-mode>
    </session-config>
 </web-app>

我找不到关于 "multiple tracking-mode elements" 的附加信息。如果我有以下情况,Tomcat 的行为可能是什么?

<web-app>
    <session-config>
        <tracking-mode>URL</tracking-mode>
        <tracking-mode>COOKIE</tracking-mode>
    </session-config>
 </web-app>

问题的答案可以在org.apache.catalina.connector.CoyoteAdapter#postParseRequest

中找到

以下代码来自tomcat8.5:

            String sessionID;
            if (request.getServletContext().getEffectiveSessionTrackingModes()
                    .contains(SessionTrackingMode.URL)) {

                // Get the session ID if there was one
                sessionID = request.getPathParameter(
                        SessionConfig.getSessionUriParamName(
                                request.getContext()));
                if (sessionID != null) {
                    request.setRequestedSessionId(sessionID);
                    request.setRequestedSessionURL(true);
                }
            }

            // Look for session ID in cookies and SSL session
            parseSessionCookiesId(request);
            parseSessionSslId(request);

            sessionID = request.getRequestedSessionId();

发生以下情况:

  1. 如果允许 tomcat 使用 URL 会话跟踪,它会尝试在 URL 请求中找到一个 sessionId
  2. 如果允许使用 cookie 跟踪 - 它会在 cookie 中查找会话 ID。它优先,无论请求中是否有会话 ID。
  3. (不是您问题的一部分,但为了完整起见)当且仅当它是唯一允许的跟踪模式时,才会使用 SSL 会话跟踪。否则将被忽略。

我不知道为什么 URL 跟踪没有像 SSL 和 Cookie 跟踪模式那样以单独的方法提取,但它们看起来几乎相同:

  1. 检查模式是否开启
  2. 尝试查找 SessionId
  3. 在请求对象中设置会话 ID。

Tomcat 7 的 tracking-mode 功能已替换 tomcat 6's disableURLRewriting

Context.disableURLRewriting: This has been removed. An equivalent effect can be obtained by configuring the session-config/tracking-mode elements in a web application or in the global CATALINA_BASE/conf/web.xml file.

你可以Disable JSESSIONID in URL

Servlet 3.0 standard gives you two ways to disable URL session rewriting. This works in Tomcat 7, Glassfish v3, and any other Servlet 3.0-compliant servlet container