How do you configure HttpOnly cookies in tomcat / java webapps?

后端 未结 9 1007
迷失自我
迷失自我 2020-11-27 10:47

After reading Jeff\'s blog post on Protecting Your Cookies: HttpOnly. I\'d like to implement HttpOnly cookies in my web application.

How do you tell tomcat to use ht

9条回答
  •  一整个雨季
    2020-11-27 11:21

    Please be careful not to overwrite the ";secure" cookie flag in https-sessions. This flag prevents the browser from sending the cookie over an unencrypted http connection, basically rendering the use of https for legit requests pointless.

    private void rewriteCookieToHeader(HttpServletRequest request, HttpServletResponse response) {
        if (response.containsHeader("SET-COOKIE")) {
            String sessionid = request.getSession().getId();
            String contextPath = request.getContextPath();
            String secure = "";
            if (request.isSecure()) {
                secure = "; Secure"; 
            }
            response.setHeader("SET-COOKIE", "JSESSIONID=" + sessionid
                             + "; Path=" + contextPath + "; HttpOnly" + secure);
        }
    }
    

提交回复
热议问题