steven35
steven35

Reputation: 4017

Add non-http only cookie in Liferay

I'm trying to add a non http only cookie in Liferay. This is my attempt in the action class

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);

The cookie is saved, I can see it in the resouces view of the browser, but it's http only so I can't read it with javascript. Any idea how I can make it non-http only?

Edit:

If I just use response.addProperty(cookie); the result is the same.

This is the response header

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

Upvotes: 2

Views: 1071

Answers (1)

steven35
steven35

Reputation: 4017

I solved this by setting the response header manually

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);

Upvotes: 1

Related Questions