jsessionid 作为路径参数在 Tomcat 中不起作用
jsessionid as path parameter not working in Tomcat
我使用的是 Tomcat 7.0.84,我的 Web 应用程序使用 Servlet 3.0 部署描述符。 web.xml 文件包含以下内容:
<session-config>
<cookie-config>
<name>JSESSIONID</name>
<http-only>false</http-only>
</cookie-config>
<tracking-mode>URL</tracking-mode>
<tracking-mode>COOKIE</tracking-mode>
</session-config>
我有一个桌面应用程序可以登录网络应用程序并建立会话。为响应用户操作,它会在浏览器中调用 URL。由于我希望浏览器使用相同的会话登录,因此我附加了 jsessionid 路径参数,如下所示:
http://server/contextroot/path/;jsessionid=8BDF744802E7850D5AA4AB6535163504
我完全关闭了浏览器,因此当 URL 生成时,不会发送之前的会话 cookie。 (我的默认浏览器是 chrome,我确认是这样的。)
我也在代码中通过记录 return 值启用了 URL 跟踪模式
ServletContext.getEffectiveSessionTrackingModes.
我期待的是浏览器请求自动获取 ;jsessionid 参数指示的会话,但它没有发生。每次 Tomcat 在其响应中包含一个新的会话 cookie。
此代码用于以前版本的 Tomcat(可能是 5.5)和 servlet 2.3 规范。我在 Servlet 3.0 规范或 Tomcat 文档中没有看到任何表明这不应该工作的内容,我完全没有想法。
有谁知道为什么这没有按预期工作?
以下是我如何让它工作的:
在web.xml中,我更改了
<cookie-config>
<name>JSESSIONID</name>
<http-only>false</http-only>
</cookie-config>
至:
<cookie-config>
<name>jsessionid</name>
<http-only>false</http-only>
</cookie-config>
因此会话 cookie 名称现在全部为小写,并且与 jsessionid 路径参数的名称完全匹配。
另一种工作方式是将路径参数名称从 jsessionid 更改为 JSESSIONID。这是因为,在 Tomcat 中,如果您为会话 cookie 显式配置一个名称,它将使用该名称作为用于传递会话 ID 的路径参数的名称。这似乎不符合 Servlet 3.0 规范的第 7.1.3 节,其中说:
The session ID must be encoded as a path parameter in the URL string.
The name of the parameter must be jsessionid. Here is an example of a
URL containing encoded path information:
但是,它确实符合第 7.1.1 节的摘录:
If a web application configures a custom name for its session tracking
cookies, the same custom name will also be used as the name of the URI
parameter if the session id is encoded in the URL (provided that URL
rewriting has been enabled).
我使用的是 Tomcat 7.0.84,我的 Web 应用程序使用 Servlet 3.0 部署描述符。 web.xml 文件包含以下内容:
<session-config>
<cookie-config>
<name>JSESSIONID</name>
<http-only>false</http-only>
</cookie-config>
<tracking-mode>URL</tracking-mode>
<tracking-mode>COOKIE</tracking-mode>
</session-config>
我有一个桌面应用程序可以登录网络应用程序并建立会话。为响应用户操作,它会在浏览器中调用 URL。由于我希望浏览器使用相同的会话登录,因此我附加了 jsessionid 路径参数,如下所示:
http://server/contextroot/path/;jsessionid=8BDF744802E7850D5AA4AB6535163504
我完全关闭了浏览器,因此当 URL 生成时,不会发送之前的会话 cookie。 (我的默认浏览器是 chrome,我确认是这样的。)
我也在代码中通过记录 return 值启用了 URL 跟踪模式 ServletContext.getEffectiveSessionTrackingModes.
我期待的是浏览器请求自动获取 ;jsessionid 参数指示的会话,但它没有发生。每次 Tomcat 在其响应中包含一个新的会话 cookie。
此代码用于以前版本的 Tomcat(可能是 5.5)和 servlet 2.3 规范。我在 Servlet 3.0 规范或 Tomcat 文档中没有看到任何表明这不应该工作的内容,我完全没有想法。
有谁知道为什么这没有按预期工作?
以下是我如何让它工作的:
在web.xml中,我更改了
<cookie-config>
<name>JSESSIONID</name>
<http-only>false</http-only>
</cookie-config>
至:
<cookie-config>
<name>jsessionid</name>
<http-only>false</http-only>
</cookie-config>
因此会话 cookie 名称现在全部为小写,并且与 jsessionid 路径参数的名称完全匹配。
另一种工作方式是将路径参数名称从 jsessionid 更改为 JSESSIONID。这是因为,在 Tomcat 中,如果您为会话 cookie 显式配置一个名称,它将使用该名称作为用于传递会话 ID 的路径参数的名称。这似乎不符合 Servlet 3.0 规范的第 7.1.3 节,其中说:
The session ID must be encoded as a path parameter in the URL string. The name of the parameter must be jsessionid. Here is an example of a URL containing encoded path information:
但是,它确实符合第 7.1.1 节的摘录:
If a web application configures a custom name for its session tracking cookies, the same custom name will also be used as the name of the URI parameter if the session id is encoded in the URL (provided that URL rewriting has been enabled).