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屬性
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 {
- }
- }
- 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
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.
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
- <Context useHttpOnly="true"></context>
- <session-config>
- <session-timeout>30</session-timeout>
- <cookie-config>
- <http-only>true</http-only>
- </cookie-config>
- </session-config>
- <Connector port="8080" protocol="HTTP/1.1"
- connectionTimeout="20000"
- redirectPort="8443" secure="true" />