Java開發 | 安全篇 Cookie設置secure屬性


Java開發 | 安全篇 Cookie設置secure屬性

What is it and why do I care ?

Session cookies (或者包含JSSESSIONID的cookie)是指用來管理web應用的session會話的cookies.這些cookie中保存特定使用者的session ID標識,而且相同的session ID以及session生命周期內相關的數據也在服務器端保存。在web應用中最常用的session管理方式是通過每次請求的時候將cookies傳送到服務器端來進行session識別。

你可以設置附加的secure標識來提示瀏覽器只能通過Https(加密方式)方式來傳輸cookie,Http(未加密方式)方式則不可以。這種方式來保證你的session cookie對於攻擊者是不可見的,避免中間人攻擊(Man-in-the-Middle Attack,簡稱“MITM攻擊”)。這並不是一個完美的session安全管理方案,卻是一個重要的步驟。

what should I do about it ?

應對方法很簡單。你必須在session cookie添加secure標識(如果有可能的話最好保證請求中的所有cookies都是通過Https方式傳輸)

如下是示例:未添加secure標識的session cookie-可能會被泄露

Cookie: jsessionid=AS348AF929FK219CKA9FK3B79870H;

添加secure標識:

Cookie: jsessionid=AS348AF929FK219CKA9FK3B79870H; secure;

方式很簡潔。你可以甚至可以手工設置這個標識,如果你在Servlet3或者更新的環境中開發,只需要在web.xml簡單的配置來實現。你只要在web.xml中添加如下片段:

 

<session-config>
  <cookie-config>
    <secure>true</secure>
  </cookie-config>
</session-config>

 

___________________________________________________________________________________________________

 

Java 開發 | 安全篇 設置Cookie 的HttpOnly屬性

https://coder-programming.blog.csdn.net/article/details/79074644?spm=1001.2101.3001.6650.6&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-6.queryctrv2&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-6.queryctrv2&utm_relevant_index=13

Cookie的HttpOnly屬性說明

cookie的兩個新的屬性secure和Httponly分別表示只能通過Http訪問cookie   不能通過腳本訪問Cookie、HttpOnly屬性在一定程度上可以防止XSS攻擊(XSS攻擊類似sql注入,更多資料可以百度查閱)。在web應用中、JSESSIONID (Cookie)沒有設置Httponly屬性可能會竊取或操縱客戶會話和 cookie,它們可能用於模仿合法用戶,從而使黑客能夠以該用戶身份查看或變更用戶記錄以及執行事務、
cookie的HttpOnly屬性需要瀏覽器的支持、目前IE6/FF3.0以上均已支持。另外JavaEE6.0已支持對HttpOnly的修改、servlet3.0規范中也添加了API。
 

攔截器設置添加

我們可以配置攔截器攔截所有請求,然后再給cookie添加HttpOnly屬性
[java]   view plain   copy
 
  1. public class CookieFilter implements Filter {  
  2.     public void doFilter(ServletRequest request, ServletResponse response,  
  3.             FilterChain chain) throws IOException, ServletException {  
  4.         HttpServletRequest req = (HttpServletRequest) request;  
  5.         HttpServletResponse resp = (HttpServletResponse) response;  
  6.   
  7.         Cookie[] cookies = req.getCookies();  
  8.   
  9.         if (cookies != null) {  
  10.                 Cookie cookie = cookies[0];  
  11.                 if (cookie != null) {  
  12.                     /*cookie.setMaxAge(3600); 
  13.                     cookie.setSecure(true); 
  14.                     resp.addCookie(cookie);*/  
  15.                       
  16.                     //Servlet 2.5不支持在Cookie上直接設置HttpOnly屬性  
  17.                     String value = cookie.getValue();  
  18.                     StringBuilder builder = new StringBuilder();  
  19.                     builder.append("JSESSIONID=" + value + "; ");  
  20.                     builder.append("Secure; ");  
  21.                     builder.append("HttpOnly; ");  
  22.                     Calendar cal = Calendar.getInstance();  
  23.                     cal.add(Calendar.HOUR, 1);  
  24.                     Date date = cal.getTime();  
  25.                     Locale locale = Locale.CHINA;  
  26.                     SimpleDateFormat sdf =   
  27.                             new SimpleDateFormat("dd-MM-yyyy HH:mm:ss",locale);  
  28.                     builder.append("Expires=" + sdf.format(date));  
  29.                     resp.setHeader("Set-Cookie", builder.toString());  
  30.                 }  
  31.         }  
  32.         chain.doFilter(req, resp);  
  33.     }  
  34.   
  35.     public void destroy() {  
  36.     }  
  37.   
  38.     public void init(FilterConfig arg0) throws ServletException {  
  39.     }  
  40. }  
此段代碼摘自  CookieFilter 這樣我們吧所有的cookie都添加上了HttpOnly屬性。
注:需要servlet3.0支持、Tomcat7木有問題。查看servlet的版本方法:
知道到Tomcat/lib 文件夾下servlet-api.jar 將其解壓、然后打開servlet-api\META-INF\MANIFEST.MF文件(Editplus/NotePad++等工具都行)、
[plain]   view plain   copy
 
  1. Manifest-Version: 1.0  
  2. Ant-Version: Apache Ant 1.9.3  
  3. Created-By: 1.6.0_45-b06 (Sun Microsystems Inc.)  
  4. X-Compile-Source-JDK: 1.6  
  5. X-Compile-Target-JDK: 1.6  
  6.   
  7. Name: javax/servlet/  
  8. Specification-Title: Java API for Servlets  
  9. <span style="color:#ff0000;">Specification-Version: 3.0</span>  
  10. Specification-Vendor: Sun Microsystems, Inc.  
  11. Implementation-Title: javax.servlet  
  12. Implementation-Version: 3.0.FR  
  13. Implementation-Vendor: Apache Software Foundation  
紅色字體就是servlet版本。  參考資料:  查看servlet/jsp版本
這種配置攔截器通過response給cookie添加HttpOnly屬性、在某種情況下並太不合理、而且可能對項目有寫影響、我的項目在這么做之后再Google瀏覽器沒有問題,但在FF和IE上、發現了問題。我們項目頁面用了tiles框架布局,在LoginAction登錄返回到struts result配置跳轉到tiles、teles再自己發送請求加載數據、問題就出現在這里、此時發送的請求與之前發送的請求現在為不同session、導致出錯。屏蔽CookieFiter后沒問題、因此猜想是因為HttpOnly屬性的影響使session改變了。
 

Tomcat配置Jsessionid HttpOnly屬性

在部分web項目中、基本沒有手動操作的cookie、只有會話Tomcat的jsessionid的cookie。這中情況我們就可以通過Tomcat配置來實現jsessionid默認HttpOnly屬性值。
useHttpOnly Should the HttpOnly flag be set on session cookies to prevent client side script from accessing the session ID? Defaults to false.

Tomcat6官方文檔

useHttpOnly Should the HttpOnly flag be set on session cookies to prevent client side script from accessing the session ID? Defaults to false.

Tomcat7官方文檔

useHttpOnlyShould the HttpOnly flag be set on session cookies to prevent client side script from accessing the session ID? Defaults to true.

從文檔來看tomcat6及5.5useHttpOnly 默認是false、7則是默認true

修改tomcat/conf/context.xml
[plain]   view plain   copy
 
  1. <Context useHttpOnly="true"></context>  
修改tomcat/conf/web.xml
[plain]   view plain   copy
 
  1. <session-config>  
  2.         <session-timeout>30</session-timeout>  
  3.     <cookie-config>  
  4.             <http-only>true</http-only>  
  5.         </cookie-config> 
  6.  </session-config>  
網上大部分資料只配置以上、但實測卻發現沒有
其實、還要配置secure屬性
修改tomcat/conf/server.xml
[plain]   view plain   copy
 
  1. <Connector port="8080" protocol="HTTP/1.1"  
  2.                connectionTimeout="20000"  
  3.                redirectPort="8443" secure="true" />
給8080端口啟用安全、這樣啟動Tomcat訪問項目發現HttpOnly及secure屬性都已經啟動
 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM