在eclipese中创建Web工程时,有个dynamic web module version选项,首先解释下这个选项的意思:

http://stackoverflow.com/questions/3985916/dynamic-web-module-option-in-eclipse

That version correlates with Servlet API version. Servlet 3.0 (released at december 2009 as part of Java EE 6) runs on Servlet 3.0 containers only (Tomcat 7, Glassfish 3, JBoss AS 6, etc). Servlet 2.5 (released at 11 may 2006 as part of Java EE 5) runs on Servlet 2.5 containers only or newer (Tomcat 6, Glassfish 2, JBoss AS 5, etc). Servlet 2.4 (released at november 2003 as part of J2EE 1.4) runs on Servlet 2.4 containers only or newer, etcetera.

You just need to pick the right API version whatever you want to implement your webapp in. Or if you don't have the freedom in picking the servlet Container used, then pick the API which suits the servlet container version the best.

As to why the JDK defaults to one or other, it's just the minimum JDK requirement of the Servlet API version in question. Often, when you're picking an older Servlet API, in reality the JRE/JDK used is also that old.

Tomcat版本为6.0.39,JDK版本为1.6update45

在Web工程上增加一个Filter对Cookie进行处理

  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. }

web.xml:

  1. <filter>
  2. <filter-name>cookieFilter</filter-name>
  3. <filter-class>com.sean.CookieFilter</filter-class>
  4. </filter>
  5. <filter-mapping>
  6. <filter-name>cookieFilter</filter-name>
  7. <url-pattern>/*</url-pattern>
  8. </filter-mapping>

FireFox:

Chrome:

IE:

---------------------------------------------------------------------------------------------------------------------------

 介绍另外一种办法:
利用HttpResponse的addHeader方法,设置Set-Cookie的值
cookie字符串的格式:key=value; Expires=date; Path=path; Domain=domain; Secure; HttpOnly

//设置cookie

response.addHeader("Set-Cookie", "uid=112; Path=/; HttpOnly");

//设置多个cookie

response.addHeader("Set-Cookie", "uid=112; Path=/; HttpOnly");

response.addHeader("Set-Cookie", "timeout=30; Path=/test; HttpOnly");

//设置https的cookie

response.addHeader("Set-Cookie", "uid=112; Path=/; Secure; HttpOnly");

在实际使用中,我们可以使FireCookie查看我们设置的Cookie 是否是HttpOnly

 

Cookie设置HttpOnly,Secure,Expire属性的更多相关文章

  1. 浏览器因cookie设置HttpOnly标志引起的安全问题

    1.简介 如果cookie设置了HttpOnly标志,可以在发生XSS时避免JavaScript读取cookie,这也是HttpOnly被引入的 原因.但这种方式能防住攻击者吗?HttpOnly标志可 ...

  2. Servlet 2.5为cookie配置HTTPOnly属性

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

  3. Session Cookie的HttpOnly和secure属性

    Session Cookie的HttpOnly和secure属性 一.属性说明: 1 secure属性 当设置为true时,表示创建的 Cookie 会被以安全的形式向服务器传输,也就是只能在 HTT ...

  4. Cookie的HttpOnly、secure、domain属性

    Cookie主要属性 Cookie主要属性: path domain max-age expires:是expires的补充,现阶段有兼容性问题:IE低版本不支持,所以一般不单独使用 secure h ...

  5. cookie 的HttpOnly 和 Secure 属性

    设置HttpOnly=true的cookie不能被js获取到,无法用document.cookie打出cookie的内容. Secure属性是说如果一个cookie被设置了Secure=true,那么 ...

  6. PHP设置COOKIE的HttpOnly属性

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

  7. Appscan漏洞 之 加密会话(SSL)Cookie 中缺少 Secure 属性

    近期 Appscan扫描出漏洞 加密会话(SSL)Cookie 中缺少 Secure 属性,已做修复,现进行总结如下: 1.1.攻击原理 任何以明文形式发送到服务器的 cookie.会话令牌或用户凭证 ...

  8. 关于Cookie 的HttpOnly属性(java/web操作cookie+Tomcat操作jsessionid)

    关于Cookie的其它只是不在累述.本文主要讲讲自己在项目中遇到的cookie的HttpOnly属性问题 Cookie的HttpOnly属性说明 cookie的两个新的属性secure和Httponl ...

  9. php设置cookie为httponly防止xss攻击

    什么是XSS攻击? XSS攻击(Cross Site Scripting)中文名为跨站脚本攻击,XSS攻击时web中一种常见的漏洞.通过XSS漏洞可以伪造目标用户登录,从而获取登录后的账号操作. 网站 ...

随机推荐

  1. Oracle SGA参数调整

    一. SGA的组成: 自动 SGA 管理后,Oracle 可以自动为我们调整以下内存池的大小: shared pool buffer cache large pool java pool stream ...

  2. 解决Win7系统安装时“安装程序无法定位现有 系统分区,也无法创建新的系统分区”提示

    第一步:U盘启动装系统时,格式化主分区的内容后出现上面的问题 第二步:重启机器,通过U 盘启动.进入win pe系统. 第三步:把windows 7的ISO镜像解压到电脑的非系统盘的其他硬盘上.如D: ...

  3. Python问题之奇怪诡异的Bug

    最近又重新装上了windows 7感觉还是那样,主要是想用M8SDK写些程序.也想在windows上玩玩,一直都觉得用C写一些常用的东东很复杂,只有借助于解释性语言了,在python, ruby间选择 ...

  4. 《WPF程序设计指南》读书笔记——第2章 基本画刷

    1.Color结构 using System; using System.Windows; using System.Windows.Input; using System.Windows.Media ...

  5. K2 Blackpearl 4.6.8 安装步骤详解

    由于某些原因,我幼小的心灵受到了很大的创伤,倍感世态之炎凉,久久不能愈合,也因此很久没再接触K2 Blackpearl了.偶然来了兴趣,想整个K2的环境,闲暇之余了解其新功能,温故知新,也希望从中能讨 ...

  6. iOS中touches事件,addtarget ...action和GestureRecognizer详解

    刚学完uiview,uicontrol类,许多人知道 touchesBegain,touchesMoved,touchesEnd,GestureRecognizer的用途,但仔细考虑这些事件之间的关系 ...

  7. 【BZOJ 1189】[HNOI2007]紧急疏散evacuate

    Description 发生了火警,所有人员需要紧急疏散!假设每个房间是一个N M的矩形区域.每个格子如果是'.',那么表示这是一块空地:如果是'X',那么表示这是一面墙,如果是'D',那么表示这是一 ...

  8. Calendar GData API / Google Calendar Connectors deprecation

    http://googleappsupdates.blogspot.fr/2014/06/calendar-gdata-api-google-calendar.html

  9. 闭包(Closures)

    浅析 JavaScript 中的闭包(Closures) 一.前言 对于 JavaScript 来说,闭包是一个非常强大的特征.但对于刚开始接触的初学者来说它又似乎是特别高深的.今天我们一起来揭开闭包 ...

  10. bnuoj 1071 拼图++(BFS+康拓展开)

    http://www.bnuoj.com/bnuoj/problem_show.php?pid=1071 [题意]:经过四个点的顺逆时针旋转,得到最终拼图 [题解]:康拓展开+BFS,注意先预处理,得 ...