[英]How do you configure HttpOnly cookies in tomcat / java webapps?
阅读 Jeff 关于保护您的 Cookie:HttpOnly的博客文章后。 我想在我的 Web 应用程序中实现 HttpOnly cookie。
你如何告诉 tomcat 在会话中只使用 http cookie?
更新:此处的 JSESSIONID 内容仅适用于较旧的容器。 请使用 jt 当前接受的答案,除非您使用 < Tomcat 6.0.19 或 < Tomcat 5.5.28 或其他不支持 HttpOnly JSESSIONID cookie 作为配置选项的容器。
在您的应用程序中设置 cookie 时,请使用
response.setHeader( "Set-Cookie", "name=value; HttpOnly");
但是,在许多 webapp 中,最重要的 cookie 是会话标识符,它由容器自动设置为 JSESSIONID cookie。
如果只使用这个cookie,可以写一个ServletFilter在出路时重新设置cookie,强制JSESSIONID为HttpOnly。
http://keepitlocked.net/archive/2007/11/05/java-and-httponly.aspx
http://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-only 设置可能是一个可接受的快速修复。
请注意,随着您的代码随着时间的推移而发展,当您忘记此过滤器并尝试在代码中的其他位置设置另一个 cookie 时,会有一个令人讨厌的隐藏错误等着您。 当然,它不会被设置。
这确实是一个黑客。 如果您确实使用 Tomcat 并且可以编译它,那么请查看 Shabaz 的极好建议,将 HttpOnly 支持修补到 Tomcat 中。
请注意不要覆盖 https 会话中的 ";secure" cookie 标志。 此标志可防止浏览器通过未加密的 http 连接发送 cookie,基本上使对合法请求使用 https 变得毫无意义。
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 服务器支持 Serlvet 3.0 规范,例如 tomcat 7.0+,您可以在web.xml
使用以下内容:
<session-config>
<cookie-config>
<http-only>true</http-only>
<secure>true</secure>
</cookie-config>
</session-config>
如文档中所述:
HttpOnly :指定此 Web 应用程序创建的任何会话跟踪 cookie 是否将标记为 HttpOnly
Secure :指定即使发起相应会话的请求使用普通 HTTP 而不是 HTTPS,此 Web 应用程序创建的任何会话跟踪 cookie 是否也将标记为安全
对于会话 cookie,Tomcat 似乎尚不支持它。 请参阅错误报告需要添加对 HTTPOnly 会话 cookie 参数的支持。 现在可以在这里找到一个有点复杂的解决方法,它基本上归结为手动修补 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);
}
org.apache.catalina.connectorResponse.addCookieInternal
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);
}
对于我明确设置的 cookie,我改用了Apache Shiro提供的SimpleCookie 。 它不是从 javax.servlet.http.Cookie 继承的,所以它需要更多的杂耍才能使一切正常工作,但是它确实提供了一个属性集 HttpOnly 并且它可以与 Servlet 2.5 一起使用。
要在响应上设置 cookie,而不是执行response.addCookie(cookie)
您需要执行cookie.saveTo(request, response)
。
还应该注意的是,打开 HttpOnly 会破坏需要有状态访问回 jvm 的小程序。
Applet http 请求不会使用 jsessionid cookie,并且可能会被分配给不同的 tomcat。
我在OWASP 中找到
<session-config>
<cookie-config>
<http-only>true</http-only>
</cookie-config>
</session-config>
这也是“配置中的httponlycookies”安全问题的修复
在 Tomcat6 中,您可以有条件地从您的 HTTP 侦听器类启用:
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 7.x/8.x/9.x
转到 Tomcat >> conf 文件夹打开 web.xml 并在 session-config 部分添加以下内容
<cookie-config>
<http-only>true</http-only>
<secure>true</secure>
</cookie-config>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.