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

关于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属性
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++等工具都行)、
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
<Context <span style="background-color: rgb(255, 204, 51);">useHttpOnly="true"</span>></context>
修改tomcat/conf/web.xml
<session-config>
        <session-timeout>30</session-timeout>
	<span style="background-color: rgb(255, 204, 51);"><cookie-config>
            <http-only>true</http-only>
        </cookie-config></span>
    </session-config>
网上大部分资料只配置以上、但实测却发现没有
其实、还要配置secure属性
修改tomcat/conf/server.xml
<Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" <span style="background-color: rgb(255, 204, 51);">secure="true"</span>/>
给8080端口启用安全、这样启动Tomcat访问项目发现HttpOnly及secure属性都已经启动



  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值