在Liferay中添加非仅http cookie

Add non-http only cookie in Liferay

本文关键字:http cookie 添加 Liferay      更新时间:2023-09-26

我正试图在Liferay中添加一个非http cookie。这是我在动作类中的尝试

Cookie cookie = new Cookie("testName", "testValue");
cookie.setMaxAge(60 * 60);
cookie.setPath("/");
cookie.setVersion(0);
cookie.setHttpOnly(false);
cookie.setSecure(false);
CookieKeys.addCookie(PortalUtil.getHttpServletRequest(request), PortalUtil.getHttpServletResponse(response), cookie, false);

cookie被保存了,我可以在浏览器的资源视图中看到它,但它只是http,所以我不能用javascript读取它。你知道我怎样才能使它成为非http的吗?

编辑:

如果我只使用response.addProperty(cookie);,结果是一样的。

这是响应标头

Set-Cookie: testName=testValue; Expires=Mon, 07-Dec-2015 15:15:27 GMT; Path=/; HttpOnly

我通过手动设置响应标头解决了这个问题

Calendar ca = Calendar.getInstance();
ca.add(Calendar.HOUR_OF_DAY, 1);
String cookieString = "testName=testValue; Expires=" + ca.getTime() + "; Path=/;";
PortalUtil.getHttpServletResponse(response).setHeader("Set-Cookie", cookieString);