簡體   English   中英

如何在 Jetty 上的 Spring 應用程序中將 jsessionid cookie 路徑更改為服務器根目錄?

[英]How to change jsessionid cookie path to server root in Spring app on Jetty?

我有一個在/app上下文上運行 Spring 應用程序的 Jetty 服務器。 該應用程序使用會話,因此它設置了一個會話 cookie,其響應如下:

set-cookie:JSESSIONID=679b6291-d1cc-47be-bbf6-7ec75214f4e5; Path=/app; HttpOnly

我需要該 cookie 具有/而不是 webapp 上下文的路徑。 另外,我想使用安全 cookie。 我想要這個回應:

set-cookie:JSESSIONID=679b6291-d1cc-47be-bbf6-7ec75214f4e5; Path=/; HttpOnly; Secure

配置會話 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>

這會導致拋出異常:

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 

完整的堆棧跟蹤在這個 gist中。

嘗試#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>

這不會導致任何異常,但 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>

這不會導致任何異常,但 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>

這不會導致任何異常,但 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>

嘗試#4是在正確的軌道上。

提供我正在閱讀此權限,您正在使用上下文/應用程序中的maven配置,這意味着在您的web.xml中/您的設置 / app,因為這是您正在配置的上下文的根。

換句話說,如果您只是部署到www.foo.com/app上下文中,則無法為www.foo.com/配置會話,想象一下如果其他人正在將應用程序部署到該URL中,您不能僅僅決定使您的會話cookie適用於在該URL下運行的每個人。

為了我的理智,我只以編程方式配置 Jetty。 如果您要更改 Jetty 11 中的 cookie 路徑:

sessionHandler.getSessionCookieConfig().setPath("/");

如果您沒有顯式創建 SessionHandler,則可以在創建諸如 servlet 上下文之類的內容后獲取對它的引用...。

ServletContextHandler sch = new ServletContextHandler(..., "/api", true, false);
sch.getSessionHandler().getSessionCookieConfig().setPath("/");

這樣,即使上下文位於/api下,會話 cookie 也可以在其他上下文中訪問。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM