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属性都已经启动
 

 

posted @ 2022-01-18 11:42  kelelipeng  阅读(5995)  评论(0编辑  收藏  举报