2012-08-01 41 views
5

我做了很多googeling並找不到答案。我曾嘗試設置在web.xml文件中的以下在沒有影響戰爭:如何將會話cookie標記爲安全(僅限https)在tomcat 6中

<session-config> 
     <session-timeout>60</session-timeout> 
     <cookie-config> 
      <http-only>true</http-only> 
      <secure>true</secure> 
     </cookie-config> 
    </session-config> 

在Tomcat context.xml文件添加useHttpOnly起限制餅乾僅對HTTP類型的,但我仍然需要保證其安全。

回答

4

你不必做任何事情。只要啓動會話的請求是https Tomcat將會話cookie標記爲secure

我也看了看是否有任何正式記錄的事實,但我找不到它。但那是至少Tomcat 6.0.32及更高版本的行爲。

下面是從org/apache/catalina/connector/Request.java的代碼,在方法的末尾,檢查是否請求是安全的,如果是,設置在cookie中的secure標誌:

/** 
* Configures the given JSESSIONID cookie. 
* 
* @param cookie The JSESSIONID cookie to be configured 
*/ 
protected void configureSessionCookie(Cookie cookie) { 
    cookie.setMaxAge(-1); 

    Context ctxt = getContext(); 

    String contextPath = null; 
    if (ctxt != null && !getConnector().getEmptySessionPath()) { 
     if (ctxt.getSessionCookiePath() != null) { 
      contextPath = ctxt.getSessionCookiePath(); 
     } else { 
      contextPath = ctxt.getEncodedPath(); 
     } 
    } 
    if ((contextPath != null) && (contextPath.length() > 0)) { 
     cookie.setPath(contextPath); 
    } else { 
     cookie.setPath("/"); 
    } 

    if (ctxt != null && ctxt.getSessionCookieDomain() != null) { 
     cookie.setDomain(ctxt.getSessionCookieDomain()); 
    } 

    if (isSecure()) { 
     cookie.setSecure(true); 
    } 
} 

更新:您可以手動嘗試自行使用過濾器等設置這個..你可以檢查從 set 'secure' flag to JSESSION id cookie

+0

的例子只是想確認:在我的情況下,你的選擇(Safari的Webinspector的瀏覽器開發工具,查看您的會話cookie )會告訴你,該cookie確實是一個安全的cookie – 2013-09-20 15:28:02