略微加速

略速 - 互联网笔记

Session Cookie的HttpOnly和secure属性

2021-01-22 leiting (1490阅读)

标签 HTML/CSS/XML JAVA

一、属性说明:
1 secure属性
当设置为true时,表示创建的 Cookie 会被以安全的形式向服务器传输,也就是只能在 HTTPS 连接中被浏览器传递到服务器端进行会话验证,如果是 HTTP 连接则不会传递该信息,所以不会被窃取到Cookie 的具体内容。
2 HttpOnly属性
如果在Cookie中设置了"HttpOnly"属性,那么通过程序(JS脚本、Applet等)将无法读取到Cookie信息,这样能有效的防止XSS攻击。

对于以上两个属性,
首先,secure属性是防止信息在传递的过程中被监听捕获后信息泄漏,HttpOnly属性的目的是防止程序获取cookie后进行攻击。
其次,GlassFish2.x支持的是servlet2.5,而servlet2.5不支持Session Cookie的"HttpOnly"属性。不过使用Filter做一定的处理可以简单的实现HttpOnly属性。GlashFish3.0(支持servlet3.0)默认开启Session Cookie的HttpOnly属性。
也就是说两个属性,并不能解决cookie在本机出现的信息泄漏的问题(FireFox的插件FireBug能直接看到cookie的相关信息)。

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中添加如下片段:

  <!-- Cookie设置secure属性,提示浏览器只能通过Https(加密方式)方式来传输cookie,
  Http(未加密方式)方式则不可以 -->
  <session-config>
    <cookie-config>
      <http-only>true</http-only>
      <secure>true</secure>
    </cookie-config>
  </session-config>

Filter:

import javax.servlet.*;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * @Description: Cookie设置HttpOnly,Secure,Expire属性
 * @Author Tongshan.Han@partner.bmw.com
 * @Date 2018/10/24 21:57
 */

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; ");
                builder.append("Path=/; ");
                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 {
    }

}```

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3