关于Cookie的其它只是不在累述、本文主要讲讲自己在项目中遇到的cookie的HttpOnly属性问题

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属性
  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. Cookie[] cookies = req.getCookies();
  7. if (cookies != null) {
  8. Cookie cookie = cookies[0];
  9. if (cookie != null) {
  10. /*cookie.setMaxAge(3600);
  11. cookie.setSecure(true);
  12. resp.addCookie(cookie);*/
  13. //Servlet 2.5不支持在Cookie上直接设置HttpOnly属性
  14. String value = cookie.getValue();
  15. StringBuilder builder = new StringBuilder();
  16. builder.append("JSESSIONID=" + value + "; ");
  17. builder.append("Secure; ");
  18. builder.append("HttpOnly; ");
  19. Calendar cal = Calendar.getInstance();
  20. cal.add(Calendar.HOUR, 1);
  21. Date date = cal.getTime();
  22. Locale locale = Locale.CHINA;
  23. SimpleDateFormat sdf =
  24. new SimpleDateFormat("dd-MM-yyyy HH:mm:ss",locale);
  25. builder.append("Expires=" + sdf.format(date));
  26. resp.setHeader("Set-Cookie", builder.toString());
  27. }
  28. }
  29. chain.doFilter(req, resp);
  30. }
  31. public void destroy() {
  32. }
  33. public void init(FilterConfig arg0) throws ServletException {
  34. }
  35. }
public class CookieFilter implements Filter {
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse resp = (HttpServletResponse) response; Cookie[] cookies = req.getCookies(); if (cookies != null) {
Cookie cookie = cookies[0];
if (cookie != null) {
/*cookie.setMaxAge(3600);
cookie.setSecure(true);
resp.addCookie(cookie);*/ //Servlet 2.5不支持在Cookie上直接设置HttpOnly属性
String value = cookie.getValue();
StringBuilder builder = new StringBuilder();
builder.append("JSESSIONID=" + value + "; ");
builder.append("Secure; ");
builder.append("HttpOnly; ");
Calendar cal = Calendar.getInstance();
cal.add(Calendar.HOUR, 1);
Date date = cal.getTime();
Locale locale = Locale.CHINA;
SimpleDateFormat sdf =
new SimpleDateFormat("dd-MM-yyyy HH:mm:ss",locale);
builder.append("Expires=" + sdf.format(date));
resp.setHeader("Set-Cookie", builder.toString());
}
}
chain.doFilter(req, resp);
} public void destroy() {
} public void init(FilterConfig arg0) throws ServletException {
}
}

此段代码摘自CookieFilter 这样我们吧所有的cookie都添加上了HttpOnly属性。

注:需要servlet3.0支持、Tomcat7木有问题。查看servlet的版本方法:
知道到Tomcat/lib 文件夹下servlet-api.jar 将其解压、然后打开servlet-api\META-INF\MANIFEST.MF文件(Editplus/NotePad++等工具都行)、
  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. Name: javax/servlet/
  7. Specification-Title: Java API for Servlets
  8. <span style="color:#ff0000;">Specification-Version: 3.0</span>
  9. Specification-Vendor: Sun Microsystems, Inc.
  10. Implementation-Title: javax.servlet
  11. Implementation-Version: 3.0.FR
  12. Implementation-Vendor: Apache Software Foundation
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.9.3
Created-By: 1.6.0_45-b06 (Sun Microsystems Inc.)
X-Compile-Source-JDK: 1.6
X-Compile-Target-JDK: 1.6 Name: javax/servlet/
Specification-Title: Java API for Servlets
<span style="color:#ff0000;">Specification-Version: 3.0</span>
Specification-Vendor: Sun Microsystems, Inc.
Implementation-Title: javax.servlet
Implementation-Version: 3.0.FR
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
  1. <Context <span style="rgb(255, 204, 51);">useHttpOnly="true"</span>></context>
<Context <span style="">useHttpOnly="true"</span>></context>

修改tomcat/conf/web.xml

  1. <session-config>
  2. <session-timeout>30</session-timeout>
  3. <span style="rgb(255, 204, 51);"><cookie-config>
  4. <http-only>true</http-only>
  5. </cookie-config></span>
  6. </session-config>
<session-config>
<session-timeout>30</session-timeout>
<span style=""><cookie-config>
<http-only>true</http-only>
</cookie-config></span>
</session-config>

网上大部分资料只配置以上、但实测却发现没有

其实、还要配置secure属性
修改tomcat/conf/server.xml
  1. <Connector port="8080" protocol="HTTP/1.1"
  2. connectionTimeout="20000"
  3. redirectPort="8443" <span style="rgb(255, 204, 51);">secure="true"</span>/>
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" <span style="">secure="true"</span>/>

给8080端口启用安全、这样启动Tomcat访问项目发现HttpOnly及secure属性都已经启动

关于Cookie 的HttpOnly属性(java/web操作cookie+Tomcat操作jsessionid)的更多相关文章

  1. PHP设置COOKIE的HttpOnly属性

    httponly是微软对cookie做的扩展.这个主要是解决用户的cookie可能被盗用的问题. 大家都知道,当我们去邮箱或者论坛登陆后,服务器会写一些cookie到我们的浏览器,当下次再访问其他页面 ...

  2. Servlet 2.5为cookie配置HTTPOnly属性

    cookie的HTTPOnly属性,主要是用来防止JavaScript来读取cookie,默认情况下,JavaScript可以通过document.cookie来读取cookie,这样是很不安全的.通 ...

  3. eclipse配置tomcat,并部署一个Java web项目到tomcat上

    引用链接:https://blog.csdn.net/cincoutcin/article/details/79408484 eclipse配置tomcat 1.windows——preference ...

  4. Java web学习 Cookie&&Session

    cookie&&session 会话技术 从打开一个浏览器访问某个站点,到关闭这个浏览器的整个过程,成为一次会话.会 话技术就是记录这次会话中客户端的状态与数据的. 会话技术分为Coo ...

  5. Java Web之Cookie、Session

    讲Cookie和Seesion之前,先讲一下HTTP连接其实是无序的,服务器不知道是谁在访问它.现在我们来实现一个简单的邮箱功能. 要求: 1.登录页面登录之后看到收件箱和欢迎我 2.点击收件箱看到几 ...

  6. java web(七)Cookie的简单使用

    一.概述 测试 //1.创建一个Cookie对象    //Cookie cookie1=new Cookie("name","xrk");    //2.调用 ...

  7. java web中cookie的永久创建与撤销

    一.首先是创建cookie 当在数据库中查找知道所输入的用户名和密码正确之后,就开始创建: String cb=request.getParameter("cb");//cb就是登 ...

  8. CentOS上搭建java WEB开发环境Tomcat+MySQL+JDK

    对于初学者来说,想在linux系统上搭建一个java web服务器,不知道什么方案可行, 这篇文章主要是告诉这些基础和概念相对薄弱的同学,这样搭建是可行的,大体上没问 题的,出问题也是细节问题.所以此 ...

  9. Java Web项目部署Tomcat运行出错

    1.在部署Java Web项目的过程中,启动Tomcat出现报错提示 具体报错如下: Could not load the Tomcat server configuration at \Server ...

随机推荐

  1. 113th LeetCode Weekly Contest Reveal Cards In Increasing Order

    In a deck of cards, every card has a unique integer.  You can order the deck in any order you want. ...

  2. UVA - 11922 区间反转+拼接 可持久化Treap

    题意:一开始给出一个序列\(1,2...n\),然后\(m\)次操作,每次把\([l,r]\)翻转并且拼接到序列的后面,求最后形成的序列 打个pushdown标记就好 #include<iost ...

  3. 关于zipfile解压出现的字符编码问题

    使用zipfile解压文件时,出现了中文乱码问题,具体解决方法有两个,直接上代码吧. def deco_zip(path, file_path): os.mkdir(file_path) # 方式一 ...

  4. centos 7 禅道bug管理软件部署

    2018-10-26 部署前置条件 关闭防火墙或者添加防火墙规则,使禅道端口可以通过防火墙 关闭防火墙与selinux 临时关闭防火墙 systemctl stop firewalld 永久防火墙开机 ...

  5. Docker_network相关指令

    docker network create创建docker网卡docker network create -d overlay --ip-range=192.168.2.0/24 --gateway= ...

  6. WEB 倒计时

    <%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Trans ...

  7. oracle客户端中文乱码问题的解决

    1 查看服务器端编码 select userenv('language') from dual; 我实际查看到的结果为: USERENV('LANGUAGE') ------------------- ...

  8. C++ Memory System Part2: 自定义new和delete

    在第一部分中,我们介绍了new / delete的具体用法和背后的实现细节,这次我们将构建我们自己的小型工具集,可以使用我们自定义的allocator类来创建任意类型的实例(或者实例数组),我们需要做 ...

  9. 《腾讯游戏人生》微信小程序开发总结

    为打通游戏人生擂台赛与线下商家的O2O衔接,同时响应时下日臻火热的微信小程序,项目团队决定也开发一款针对性的微信小程序,以此方便商家在我们平台入驻并进行擂台赛事的创建和奖励的核销,进一步推广擂台赛的玩 ...

  10. 一个简单问题引发对IEnumerable和IQueryable的思考

    问题概述:    首先看下图,有客户表和客户负责人表关系是多对多,访问数据库使用的是EF所以这里我们开启了延迟加载,需求就是将每个客户的所有负责人逗号拼接显示在负责人这一栏位, 对你没看错需求就是这么 ...