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

Java 如何在Jetty上的Spring应用程序中将jsessionid cookie路径更改为服务器根目录?,java,spring,jetty,session-cookies,maven-jetty-plugin,Java,Spring,Jetty,Session Cookies,Maven Jetty Plugin,我有一个Jetty服务器在/app上下文中运行Spring应用程序。该应用程序使用会话,因此设置会话cookie,其响应如下: set-cookie:JSESSIONID=679b6291-d1cc-47be-bbf6-7ec75214f4e5; Path=/app; HttpOnly 我需要cookie的路径是/,而不是webapp的上下文。另外,我想使用安全的cookies。我希望得到这样的回应: set-cookie:JSESSIONID=679b6291-d1cc-47be-bbf6-

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

set-cookie:JSESSIONID=679b6291-d1cc-47be-bbf6-7ec75214f4e5; Path=/app; HttpOnly
我需要cookie的路径是
/
,而不是webapp的上下文。另外,我想使用安全的cookies。我希望得到这样的回应:

set-cookie:JSESSIONID=679b6291-d1cc-47be-bbf6-7ec75214f4e5; Path=/; HttpOnly; Secure
配置会话cookie的正确位置在哪里?春天有帮助吗?它应该在
web.xml
中吗?或者我需要以特定于容器的方式配置它,比如
jettyweb.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>
<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>
<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>
<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>
这不会导致任何异常,但cookie仍然是
JSESSIONID
,并且包含webapp上下文路径
/app

尝试#3 更新了
WEB-INF/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>
<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>
<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>
<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>
这不会导致任何异常,但cookie仍然是
JSESSIONID
,并且包含webapp上下文路径
/app

Maven配置 请注意,我正在使用Jetty Maven插件版本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>
8.1.5.v20120716
3.0.0.1版本
...
org.mortbay.jetty
jetty maven插件
${jetty.maven.plugin.version}
10
手册
${jetty.stop.port}
福
/应用程序
...
尝试4在正确的轨道上

如果我没有读错的话,那么您在context/app上使用的是maven配置,这意味着在web.xml中/your settings是/app,因为它是您正在配置的context的根

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

我尝试了4次,但cookie仍然是JSSessionID。所以请建议你如何解决你的问题。