java cookie secure,在Java Web应用程序中为设置cookie添加httponly和secure标志

I want to add the httponly and secure flags for Cookies. To implement it, I am using Filters which are configured in web.xml.

The code for adding flags is as below:

package com.crisil.dbconn;

import java.io.IOException;

import javax.servlet.Filter;

import javax.servlet.FilterChain;

import javax.servlet.FilterConfig;

import javax.servlet.ServletException;

import javax.servlet.ServletRequest;

import javax.servlet.ServletResponse;

import javax.servlet.http.Cookie;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import org.owasp.esapi.ESAPI;

import org.owasp.esapi.filters.SecurityWrapperResponse;

public class ClickjackFilter implements Filter

{

private String mode = "DENY";

/**

* Add X-FRAME-OPTIONS response header to tell IE8 (and any other browsers who

* decide to implement) not to display this content in a frame. For details, please

* refer to http://blogs.msdn.com/sdl/archive/2009/02/05/clickjacking-defense-in-ie8.aspx.

*/

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

HttpServletResponse res = (HttpServletResponse)response;

//HttpServletRequest req = (HttpServletRequest)request.getSession();

res.addHeader("X-FRAME-OPTIONS", mode );

res.addHeader("X-Content-Type-OPTIONS", "nosniff" );

res.addHeader("X-XSS-Protection", "1; mode=block" );

res.addHeader("Vary", "*" );

res.addHeader("Expires", "-1" );

res.addHeader("Pragma", "no-cache" );

res.addHeader("Cache-control", "no-cache, no-store,max-age=0, must-revalidate" );

String contextPath = ((HttpServletRequest) request).getContextPath()+"kevalcccc";

((HttpServletResponse)ServletActionContext.getResponse()).setHeader("SET-COOKIE", "JSESSIONID=" + ((HttpServletRequest)request).getSession().getId() + ";Path="+contextPath+";Secure;HttpOnly");

// touch the session

// ((HttpServletRequest) request).getSessison();

// System.out.println("zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz");

// overwriting the cookie with Secure attribute set

// ((HttpServletResponse)response).setHeader("Set-Cookie", "JSESSIONID=" + ((HttpServletRequest)request).getSession().getId() + ";Path=/");

/* Cookie[] cookies = ((HttpServletRequest) request).getCookies();

if (cookies != null)

for (int i = 0; i < cookies.length; i++) {

cookies[i].setValue("");

cookies[i].setPath("/");

cookies[i].setMaxAge(0);

cookies[i].setSecure(true);

res.addCookie(cookies[i]);

}

*/

//

String sessionid = ((HttpServletRequest) request).getSession().getId();

((HttpServletResponse) response).setHeader("SET-COOKIE", "JSESSIONID=" + sessionid + "; HttpOnly");

chain.doFilter(request, response);

}

public void destroy() {

}

public void init(FilterConfig filterConfig) {

String configMode = filterConfig.getInitParameter("mode");

if ( configMode != null ) {

mode = configMode;

}

}

}

The above code is adding httponly and secure flags for the JSESSIONID cookie. However, in the Response Header, I am getting two cookies. The second one does not have httponly and secure flags set. Please refer to the below output:

JSESSIONID=1dbLWQ6WYBHJ93Tv7TfQ2fdLgjRp2pQBsVxQVZ2WBQkYwB60wg43!1248935162!1451244054765;

HttpOnly;Secure

JSESSIONID=1dbLWQ6WYBHJ93Tv7TfQ2fdLgjRp2pQBsVxQVZ2WBQkYwB60wg43!1248935162;

path=/"

Why are the httponly and secure flags not added for the second cookie?

解决方案

Setting the JSESSIONID is the responsibility of whatever servlet container is running your web application. Remove the setHeader from your filter, and configure your web application properly by adding the following to your web.xml:

true

true

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值