如何在 Liferay portlet 中设置 Cookie?

发布于 2024-09-24 13:49:24 字数 1042 浏览 3 评论 0原文

我在尝试在 Liferay 6.0 portlet 中设置会话 cookie 时遇到问题。我希望能够向客户端浏览器设置一个 cookie,以存储用于 linkedin 身份验证的应用程序密钥,然后其他 portlet 可以在其中检索它。

我可以通过使用以下内容来读取 cookie:

public void addLinkedInCV(ActionRequest request, ActionResponse response)
        throws PortalException, SystemException {

    HttpServletRequest convertReq = PortalUtil.getHttpServletRequest(request);
    Cookie[] cookies = convertReq.getCookies();
    ...
}

这是我读取 cookie 的失败尝试。

@Override
public void doView(RenderRequest renderRequest,RenderResponse renderResponse) throws IOException, PortletException {

    HttpServletResponse convertRes = PortalUtil.getHttpServletResponse(renderResponse);
    HttpServletResponse originalRes = (HttpServletResponse) ((HttpServletResponseWrapper) convertRes).getResponse();

    Cookie linkedInCookie = new Cookie("linkedIn", util.getAppKey());
    originalRes.addCookie(linkedInCookie);
}

I am having problems of trying to set a session cookie(s) in Liferay 6.0 portlets. I want to be able to set a cookie to the client browser to store application key for the linkedin authentication, where it can be then retrieved by other portlets.

I am able to read cookies by using a following:

public void addLinkedInCV(ActionRequest request, ActionResponse response)
        throws PortalException, SystemException {

    HttpServletRequest convertReq = PortalUtil.getHttpServletRequest(request);
    Cookie[] cookies = convertReq.getCookies();
    ...
}

Here's my failed attempt to read one.

@Override
public void doView(RenderRequest renderRequest,RenderResponse renderResponse) throws IOException, PortletException {

    HttpServletResponse convertRes = PortalUtil.getHttpServletResponse(renderResponse);
    HttpServletResponse originalRes = (HttpServletResponse) ((HttpServletResponseWrapper) convertRes).getResponse();

    Cookie linkedInCookie = new Cookie("linkedIn", util.getAppKey());
    originalRes.addCookie(linkedInCookie);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神经暖 2024-10-01 13:49:24

在不大量修改 Liferay 门户本身的情况下,我发现设置 portlet cookie 的唯一方法是让 portlet 生成 JavaScript,然后让客户端设置 cookie。

因此,我将以下内容添加到 doView 方法中.

if (renderRequest.getPortletSession(true).getAttribute("set_cookie")!=null){
    return;
}

String cookie_value = renderRequest.getPortletSession(true).getId();
String cookie_hours = "6";

StringBuffer buf = new StringBuffer();
buf.append("\n <script>");
buf.append("\n var today = new Date();");
buf.append("\n var expires_date = new Date ( today.getTime() + (" + cookie_hours + "*1000*60*60) );");
buf.append("\n document.cookie = \"linkedIn=" + util.getAppKey() + ";expires=\"+expires_date.toGMTString();");    
buf.append("\n </script>");

renderResponse.setContentType("text/html");
PrintWriter out = renderResponse.getWriter();
out.println(buf.toString());
renderRequest.getPortletSession(true).setAttribute(SET_COOKIE, cookie_value);

尽管如此,这不是一个最佳的解决方案,但却是一个有效的解决方案;)

Without heavily modifying the Liferay portal itself, I found that the only way to set the portlet cookies is to have the portlet generate a javascript, which will then let the client set the cookie.

So I added the following to the doView method.

if (renderRequest.getPortletSession(true).getAttribute("set_cookie")!=null){
    return;
}

String cookie_value = renderRequest.getPortletSession(true).getId();
String cookie_hours = "6";

StringBuffer buf = new StringBuffer();
buf.append("\n <script>");
buf.append("\n var today = new Date();");
buf.append("\n var expires_date = new Date ( today.getTime() + (" + cookie_hours + "*1000*60*60) );");
buf.append("\n document.cookie = \"linkedIn=" + util.getAppKey() + ";expires=\"+expires_date.toGMTString();");    
buf.append("\n </script>");

renderResponse.setContentType("text/html");
PrintWriter out = renderResponse.getWriter();
out.println(buf.toString());
renderRequest.getPortletSession(true).setAttribute(SET_COOKIE, cookie_value);

Not an optimal, but a working solution nethertheless ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文