Đối với cookie phiên, có vẻ như nó chưa được hỗ trợ trong Tomcat. Xem báo cáo lỗi Need to add support for HTTPOnly session cookie parameter. Một công việc liên quan đến phần nào bây giờ có thể được tìm thấy here, mà về cơ bản nắm chặt để tự vá Tomcat. Không thể thực sự tìm thấy một cách dễ dàng để làm điều đó tại thời điểm này vào thời điểm này tôi đang sợ hãi.
Để tóm tắt các công việc xung quanh, nó liên quan đến tải 5,5 source, và sau đó thay đổi nguồn ở các vị trí sau:
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);
}
Tôi đang chạy Tomcat 5.5.36. Thuộc tính useHttpOnly này dường như chỉ hoạt động đối với cookie JSESSIONID. Tôi đã thêm cờ này vào tất cả các ngữ cảnh của mình để đảm bảo tất cả cookie sẽ được thêm vào "; HttpOnly" ở cuối. Tuy nhiên, chỉ JSESSIONID bị ảnh hưởng như sau: 'Set-Cookie =' JSESSIONID = 25E8F ...; Đường dẫn =/đường dẫn tùy chỉnh; HttpOnly mycustomcookie1 = xxxxxxx; Đường dẫn =/ mycustomcookie2 = 1351101062602; Đường dẫn =/ mycustomcookie3 = 0; Đường dẫn =/ mycustomcookie4 = 1; Đường dẫn = /; Bảo mật mycustomcookie5 = 4000; Hết hạn = Sat, 22-Oct-2022 17:51:02 GMT; Đường dẫn =/ Có điều gì khác tôi đang làm sai không? –
Tài liệu này dường như chỉ ra rằng cờ useHttpOnly chỉ liên quan đến cookie phiên id: http://tomcat.apache.org/tomcat-5.5-doc/config/context.html#Common_Attributes Tôi nghĩ đây là điểm dừng đo lường nhằm bảo vệ cookie phiên. Khả năng gắn cờ các cookie như HttpOnly không phải là một phần của đặc tả Servlet cho đến 3.0 (được bao phủ bởi Tomcat 7): http://today.java.net/pub/a/today/2008/10/14/introduction-to- servlet-3.html # other-linh tinh-thay đổi –