使用javax.servlet-api:4.0.1
我正在尝试使用下面的代码设置一个 httponly cookie
public static void setCookie(HttpServletResponse response,
String cookieName,
String cookieValue,
String path,
int maxAge) {
if (StringUtils.isNoneEmpty(cookieName, cookieValue)) {
Cookie cookie = new Cookie(cookieName, cookieValue);
cookie.setMaxAge(maxAge);
cookie.setPath(path);
cookie.setSecure(true);
cookie.setHttpOnly(true);
response.addCookie(cookie);
}
}
但是当检查浏览器(firefox 和 chrome)中的响应标头时,我看到路径被附加到 HttpOnly 标志,如下所示。结果路径设置不正确。
x-sny-remembermeexp=2024-05-16T20:18:08.127731+02:00; Max-Age=604800; Expires=Thu, 16 May 2024 18:18:08 GMT; Path=/; HttpOnlybe; Secure; HttpOnly
注意 Path 属性后面的
HttpOnlybe;
。 Path 属性的值应该是 /be
,但由于某种原因它被附加到 HttpOnly 标志。
添加 cookie 后检查响应标头会显示我所期望的标头
x-sny-isrememberme=true; Max-Age=604800; Expires=Thu, 16 May 2024 18:18:08 GMT; Path=/be; Secure; HttpOnly
有人以前见过这个或者知道为什么会发生这种情况吗?
将此问题归因于我们的 nginx 中的错误配置。尝试将 HttpOnly 附加到所有 cookie 时效果不佳