如何在Jetty上的Spring应用程序中将jsessionid cookie路径更改为服务器根目录?

Tau*_*ren 16 java spring jetty session-cookies maven-jetty-plugin

我有一个Jetty服务器在/app上下文中运行Spring应用程序.该应用程序使用会话,因此它设置会话cookie,响应如下:

set-cookie:JSESSIONID=679b6291-d1cc-47be-bbf6-7ec75214f4e5; Path=/app; HttpOnly
Run Code Online (Sandbox Code Playgroud)

我需要cookie来获取路径/而不是webapp的上下文.另外,我想使用安全的cookie.我想要这个回复:

set-cookie:JSESSIONID=679b6291-d1cc-47be-bbf6-7ec75214f4e5; Path=/; HttpOnly; Secure
Run Code Online (Sandbox Code Playgroud)

在哪里配置会话cookie的适当位置?春天有助于此吗?它应该在web.xml吗?或者我是否需要以容器特定的方式配置它,例如jetty-web.xml

我尝试了很多东西,但到目前为止还没有任何工作.以下是我尝试过的一些事情.


尝试#1

创建WEB-INF/jetty-web.xml如下:

<Configure class="org.eclipse.jetty.webapp.WebAppContext">
    <Get name="sessionHandler">
      <Get name="sessionManager">
        <Set name="sessionCookie">MYJETTYSESSION</Set>
        <Set name="sessionPath">/</Set>
        <Set name="secureCookies" type="boolean">true</Set>
        <Set name="httpOnly" type="boolean">true</Set>
      </Get>
    </Get>
</Configure>
Run Code Online (Sandbox Code Playgroud)

这会导致抛出异常:

2012-10-05 02:41:41.180:WARN:oejx.XmlConfiguration:Config error at <Set name="sessionPath">/</Set> java.lang.NoSuchMethodException: class org.eclipse.jetty.server.session.HashSessionManager.setSessionPath(class java.lang.String)
2012-10-05 02:41:41.180:WARN:oejx.XmlConfiguration:Config error at <Get name="sessionManager"><Set name="sessionCookie">MYJETTYSESSION</Set><Set name="sessionPath">/</Set><Set name="secureCookies">true</Set><Set name="httpOnly">true</Set></Get> java.lang.NoSuchMethodException: class org.eclipse.jetty.server.session.HashSessionManager.setSessionPath(class java.lang.String)
2012-10-05 02:41:41.180:WARN:oejx.XmlConfiguration:Config error at <Get name="sessionHandler"><Get name="sessionManager"><Set name="sessionCookie">MYJETTYSESSION</Set><Set name="sessionPath">/</Set><Set name="secureCookies">true</Set><Set name="httpOnly">true</Set></Get></Get> java.lang.NoSuchMethodException: class 
Run Code Online (Sandbox Code Playgroud)

完整的堆栈跟踪就在这个要点中.

尝试#2

创建WEB-INF/jetty-web.xml如下:

<Configure class="org.eclipse.jetty.webapp.WebAppContext">
    <Call name="setInitParameter">
        <Arg>org.eclipse.jetty.servlet.SessionCookie</Arg>
        <Arg>MYSESSIONID</Arg>
    </Call>
    <Call name="setInitParameter">
        <Arg>org.eclipse.jetty.servlet.SessionIdPathParameterName</Arg>
        <Arg>mysessionid</Arg>
    </Call>
    <Call name="setInitParameter">
        <Arg>org.eclipse.jetty.servlet.SessionPath</Arg>
        <Arg>/</Arg>
    </Call>
</Configure>
Run Code Online (Sandbox Code Playgroud)

这不会导致任何异常,但cookie仍然JSESSIONID包含webapp上下文路径/app.

尝试#3

更新WEB-INF/web.xml了以下内容:

<context-param>
    <param-name>org.eclipse.jetty.servlet.SessionPath</param-name>
    <param-value>/</param-value>
</context-param>
<context-param>
    <param-name>org.eclipse.jetty.servlet.SessionCookie</param-name>
    <param-value>MYSESS</param-value>
</context-param>
Run Code Online (Sandbox Code Playgroud)

这不会导致任何异常,但cookie仍然JSESSIONID包含webapp上下文路径/app.

尝试#4

更新WEB-INF/web.xml了以下内容:

<session-config>
    <session-timeout>720</session-timeout>
    <cookie-config>
        <name>SZSESSION</name>
        <path>/</path>
        <http-only>true</http-only>
        <secure>true</secure>
    </cookie-config>
</session-config>
Run Code Online (Sandbox Code Playgroud)

这不会导致任何异常,但cookie仍然JSESSIONID包含webapp上下文路径/app.

Maven配置

请注意,我正在使用Jetty Maven Plugin版本8.1.5.v20120716并执行以下操作mvn jetty:run:

<jetty.maven.plugin.version>8.1.5.v20120716</jetty.maven.plugin.version>
<spring.version>3.0.0.RELEASE</spring.version>
  ...
<plugin>
    <groupId>org.mortbay.jetty</groupId>
    <artifactId>jetty-maven-plugin</artifactId>
    <version>${jetty.maven.plugin.version}</version>
    <configuration>
        <scanIntervalSeconds>10</scanIntervalSeconds>
        <reload>manual</reload>
        <stopPort>${jetty.stop.port}</stopPort>
        <stopKey>foo</stopKey>
        <webAppConfig>
              <contextPath>/app</contextPath>
        </webAppConfig>
    </configuration>
       ...
</plugin>
Run Code Online (Sandbox Code Playgroud)

jes*_*ell 6

尝试#4 是在正确的轨道上。

如果我没看错,您将在上下文 /app 上使用 maven 配置,这意味着在您的 web.xml 中,您的设置/app,因为这是您正在配置的上下文的根。

换句话说,如果您仅部署到 www.foo.com/app 上下文中,则您无法为 www.foo.com/ 配置会话,想象一下,如果其他人正在将应用程序部署到该 url 中,您不能只是决定使您的会话 cookie 适用于在该 url 下操作的每个人。