阅读 Jeff 的博客文章“保护您的 Cookie:HttpOnly”后。我想在我的 Web 应用程序中实现 HttpOnly cookie。 如何告诉 tomcat 在会话中使用仅 http 的 cookie?
更新:这里的 JSESSIONID 内容是 仅适用于较旧的容器。请用 jt 目前接受的答案除非 你正在使用< Tomcat 6.0.19 or < Tomcat 5.5.28 or another container that does not support HttpOnly JSESSIONID cookies as a config option.
在您的应用程序中设置 cookie 时,请使用
response.setHeader( "Set-Cookie", "name=value; HttpOnly");
但是,在许多 Web 应用程序中,最重要的 Cookie 是会话标识符,它由容器自动设置为 JSESSIONID Cookie。
如果只使用这个cookie,可以写一个ServletFilter在出去的时候重新设置cookie,强制JSESSIONID为HttpOnly。
http://keepitlocked.net/archive/2007/11/05/java-and-httponly.aspxhttp://alexsmolen.com/blog/?p=16的页面建议添加以下内容在过滤器中。
if (response.containsHeader( "SET-COOKIE" )) {
String sessionid = request.getSession().getId();
response.setHeader( "SET-COOKIE", "JSESSIONID=" + sessionid
+ ";Path=/<whatever>; Secure; HttpOnly" );
}
但请注意,这将覆盖所有 cookie,并且仅设置您在此过滤器中指定的内容。
如果您在 JSESSIONID cookie 之外使用其他 cookie,那么您需要扩展此代码以设置过滤器中的所有 cookie。对于多个 cookie,这不是一个很好的解决方案,但对于仅 JSESSIONID 设置来说,这可能是一个可接受的快速修复。
请注意,随着您的代码随着时间的推移而发展,当您忘记此过滤器并尝试在代码中的其他位置设置另一个 cookie 时,就会有一个令人讨厌的隐藏错误在等着您。当然,它不会被设置。
这确实是一个黑客。如果您确实使用 Tomcat 并且可以编译它,那么请看看 Shabaz 的出色建议,将 HttpOnly 支持修补到 Tomcat 中。
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);
}
}
web.xml
中使用如下:
<session-config>
<cookie-config>
<http-only>true</http-only>
<secure>true</secure>
</cookie-config>
</session-config>
如文档中所述:
HttpOnly如何为java web应用程序设置httponly和session cookie:指定是否创建任何会话跟踪 cookie 此 Web 应用程序将被标记为 HttpOnly
安全:指定 此 Web 应用程序是否创建任何会话跟踪 cookie 即使发起请求的请求也将被标记为安全 相应的会话使用纯 HTTP 而不是 HTTPS
请参考
。目前,可以在here找到一个稍微复杂的解决方法,它基本上可以归结为手动修补 Tomcat。我担心目前还无法真正找到一种简单的方法来做到这一点。 总结一下解决方法,它涉及下载 5.5
源org.apache.catalina.connector.Request.java
//this is what needs to be changed
//response.addCookieInternal(cookie);
//this is whats new
response.addCookieInternal(cookie, true);
}
public void addCookieInternal(final Cookie cookie) {
addCookieInternal(cookie, false);
}
public void addCookieInternal(final Cookie cookie, boolean HTTPOnly) {
if (isCommitted())
return;
final StringBuffer sb = new StringBuffer();
//web application code can receive a IllegalArgumentException
//from the appendCookieValue invokation
if (SecurityUtil.isPackageProtectionEnabled()) {
AccessController.doPrivileged(new PrivilegedAction() {
public Object run(){
ServerCookie.appendCookieValue
(sb, cookie.getVersion(), cookie.getName(),
cookie.getValue(), cookie.getPath(),
cookie.getDomain(), cookie.getComment(),
cookie.getMaxAge(), cookie.getSecure());
return null;
}
});
} else {
ServerCookie.appendCookieValue
(sb, cookie.getVersion(), cookie.getName(), cookie.getValue(),
cookie.getPath(), cookie.getDomain(), cookie.getComment(),
cookie.getMaxAge(), cookie.getSecure());
}
//of course, we really need to modify ServerCookie
//but this is the general idea
if (HTTPOnly) {
sb.append("; HttpOnly");
}
//if we reached here, no exception, cookie is valid
// the header name is Set-Cookie for both "old" and v.1 ( RFC2109 )
// RFC2965 is not supported by browsers and the Servlet spec
// asks for 2109.
addHeader("Set-Cookie", sb.toString());
cookies.add(cookie);
}
Applet http 请求不会使用 jsessionid cookie,并且可能会被分配给不同的 tomcat。
提供的SimpleCookie。它不是从 javax.servlet.http.Cookie 继承的,因此需要更多的处理才能让一切正常工作,但它确实提供了一个属性集 HttpOnly 并且可以与 Servlet 2.5 一起使用。 要在响应上设置 cookie,您需要执行
response.addCookie(cookie)
,而不是执行
cookie.saveTo(request, response)
。中找到
<session-config>
<cookie-config>
<http-only>true</http-only>
</cookie-config>
</session-config>
这也是“配置中的httponlycookies”安全问题的修复
public void contextInitialized(ServletContextEvent event) {
if (Boolean.getBoolean("HTTP_ONLY_SESSION")) HttpOnlyConfig.enable(event);
}
使用这个类
import java.lang.reflect.Field;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import org.apache.catalina.core.StandardContext;
public class HttpOnlyConfig
{
public static void enable(ServletContextEvent event)
{
ServletContext servletContext = event.getServletContext();
Field f;
try
{ // WARNING TOMCAT6 SPECIFIC!!
f = servletContext.getClass().getDeclaredField("context");
f.setAccessible(true);
org.apache.catalina.core.ApplicationContext ac = (org.apache.catalina.core.ApplicationContext) f.get(servletContext);
f = ac.getClass().getDeclaredField("context");
f.setAccessible(true);
org.apache.catalina.core.StandardContext sc = (StandardContext) f.get(ac);
sc.setUseHttpOnly(true);
}
catch (Exception e)
{
System.err.print("HttpOnlyConfig cant enable");
e.printStackTrace();
}
}
}
转到 Tomcat >>conf 文件夹 打开 web.xml 并在会话配置部分添加以下内容
<cookie-config>
<http-only>true</http-only>
<secure>true</secure>
</cookie-config>
cookie.setHttpOnly(true);