如何在 Liferay portlet 中设置 Cookie?
我在尝试在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在不大量修改 Liferay 门户本身的情况下,我发现设置 portlet cookie 的唯一方法是让 portlet 生成 JavaScript,然后让客户端设置 cookie。
因此,我将以下内容添加到 doView 方法中.
尽管如此,这不是一个最佳的解决方案,但却是一个有效的解决方案;)
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.
Not an optimal, but a working solution nethertheless ;)