2017-07-03 122 views
3

我正在使用Spark Java與嵌入式Jetty運行應用程序。該應用程序是一個小型的網絡應用程序,根據特定的配置與多個外部服務集成。更改會話cookie名稱

要求是,客戶應該能夠運行在不同的端口在同一臺服務器多次整合的結果,即對如:應用程序在不同的端口8080和8084啓用不同的集成運行。問題是,當用戶試圖在同一個瀏覽器中訪問這兩個應用程序時會話cookie發生衝突,並且會產生問題。有沒有一種方法可以將會話變量從JSESSIONID更改爲其他內容?或者,有沒有辦法讓我知道Spark在cookie域中也包含端口?

我嘗試使用getServletContext().getSessionCookieConfig().setDomain()來設置cookie域,但getServletContext()總是返回NULL。

任何幫助,非常感謝。謝謝,從2017年三月要求

回答

2

this拉使人們有可能微調嵌入式碼頭這樣的:

public class Main { 
    public static void main(String ...args) throws Exception { 
     EmbeddedServers.add(EmbeddedServers.Identifiers.JETTY, (Routes routeMatcher, StaticFilesConfiguration staticFilesConfiguration, boolean hasMultipleHandler) -> { 
      MatcherFilter matcherFilter = new MatcherFilter(routeMatcher, staticFilesConfiguration, false, hasMultipleHandler); 
      matcherFilter.init(null); 

      JettyHandler handler = new JettyHandler(matcherFilter); 
      handler.getSessionCookieConfig().setName("XSESSION"); 

      return new EmbeddedJettyServer((int maxThreads, int minThreads, int threadTimeoutMillis) -> { 
       return new Server(); 
      }, handler); 
     }); 

     get("/hello", (req, res) -> { 
      req.session(true); 
      return "Hello World"; 
     }); 
    } 
} 

現在你可以驗證與捲曲的結果是這樣的:

curl -v localhost:4567/hello 

並由此產生的輸出會給你以下Set-Cookie標題:

Set-Cookie: XSESSION=node01j56de4fpp69kl2ye6br6cvno0.node0;Path=/ 
+0

Unfo幸運的是,該解決方案僅適用於2.6版本,但不適用於2.7版本。 – Razavi