可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
here is a function that sets a cookie:
public void addCookie(String cookieName, String cookieValue, Integer maxAge, HttpServletResponse response) {
Cookie cookie = new Cookie(cookieName, cookieValue);
cookie.setPath("/mycampaigns");
cookie.setSecure(isSecureCookie);
cookie.setMaxAge(maxAge);
response.addCookie(cookie);
}
I believe in servlet 3.0, there is a way to do this directly. Unfortunately my organization uses 2.5 and UPGRADING at this juncture IS NOT AN OPTION.
is there way to use the response to set the cookie? Here's an example i found online
response.setHeader("SET-COOKIE", "[SOME STUFF]" +"; HttpOnly")
If this is the only way to do what i want, what would i replace "[SOME STUFF]" with so that i don't lose any of the data that my function currently stores in the cookie?
回答1:
You are right, manually setting header is the right way to achive your goal.
You can also use javax.ws.rs.core.NewCookie or any other class with useful toString method to print cookie to a header to make things more simple.
public static String getHttpOnlyCookieHeader(Cookie cookie) {
NewCookie newCookie = new NewCookie(cookie.getName(), cookie.getValue(),
cookie.getPath(), cookie.getDomain(), cookie.getVersion(),
cookie.getComment(), cookie.getMaxAge(), cookie.getSecure());
return newCookie + "; HttpOnly";
}
And the usage:
response.setHeader("SET-COOKIE", getHttpOnlyCookieHeader(myOriginalCookie));
回答2:
This code works without using response.setHeader()
:
public void addCookie(String cookieName, String cookieValue, Integer maxAge, HttpServletResponse response) {
Cookie cookie = new Cookie(cookieName, cookieValue);
cookie.setPath("; HttpOnly;");
cookie.setSecure(isSecureCookie);
cookie.setMaxAge(maxAge);
response.addCookie(cookie);
}
回答3:
If you don't want to use:
response.addHeader("Set-Cookie","name=value; HttpOnly");
then you can use the below code in servlet 2.5. It will work perfect in chrome, firefox and IE11 .
Cookie cookie = new Cookie(cookieName, cookieValue);
cookie.setPath(";Path=/;HttpOnly;");
cookie.setSecure(isSecureCookie);
cookie.setMaxAge(maxAge);
response.addCookie(cookie);
Note : As you know that we don't have setHttpOnly()
method in servlet 2.5 version, so instead of this you can use:
setPath(";Path=/;HttpOnly;");
it will create a cookie with the path "/" and make the Cookie as HttpOnly
回答4:
For Java Enterprise Edition versions prior to JEE 6, say Servlet 2.5, you could find a workaround from here at OWASP. Below is an example:
/**
* Issue a cookie to the browser
*
* @param response
* @param cookieName
* @param cookieValue
* @param cookiePath
* @param maxAgeInSeconds
*/
public static void issueCookieHttpOnly(HttpServletResponse response,
String cookieName,
String cookieValue,
String cookiePath,
long maxAgeInSeconds) {
Date expireDate= new Date();
expireDate.setTime (expireDate.getTime() + (1000 * maxAgeInSeconds));
// The following pattern does not work for IE.
// DateFormat df = new SimpleDateFormat("dd MMM yyyy kk:mm:ss z");
// This pattern works for Firefox, Chrome, Safari and Opera, as well as IE.
DateFormat df = new SimpleDateFormat("EEE, dd-MMM-yyyy kk:mm:ss z");
df.setTimeZone(TimeZone.getTimeZone("GMT"));
String cookieExpire = df.format(expireDate);
StringBuilder sb = new StringBuilder(cookieName);
sb.append("=");
sb.append(cookieValue);
sb.append(";expires=");
sb.append(cookieExpire);
sb.append(";path=");
sb.append(cookiePath);
sb.append(";HttpOnly");
response.setHeader("SET-COOKIE", sb.toString());
}
回答5:
For Servlet API 2.5, you can use
response.addHeader("Set-Cookie","name=value; HttpOnly");
Be careful with the use of response.setHeader() because it deletes all the other cookies, for example, the JSESSIONID cookie.
回答6:
If you don't want to hardcode HttpOnly;
or don't like to add header, use apache shiro like this:
void addCookie(javax.servlet.http.Cookie httpCookie,
HttpServletRequest request,
HttpServletResponse response) {
org.apache.shiro.web.servlet.Cookie cookie =
new org.apache.shiro.web.servlet.SimpleCookie(httpCookie.getName());
cookie.setValue(httpCookie.getValue());
cookie.setPath(httpCookie.getPath());
// set other stuff from the original httpCookie
cookie.setHttpOnly(true);
cookie.saveTo(request, response);
}
回答7:
Spring does this using reflection without breaking on servlet 2.5 containers.
Method setHttpOnlyMethod = ReflectionUtils.findMethod(Cookie.class, "setHttpOnly", boolean.class);
if (setHttpOnlyMethod != null) {
ReflectionUtils.invokeMethod(setHttpOnlyMethod, cookie, Boolean.TRUE);
}
However setHttpOnly method is only available on Servlet 3.0 onward only.
回答8:
Cookie cookie = new Cookie("JSESSIONID", session.getId());
cookie.setPath("/");
cookie.setMaxAge(-1);
response.addCookie(cookie);
Actually in my case, this code not work exactly.
Path value are not "/"
But add this cookie.setComment("; HttpOnly;");
works fine!