Liferay Portal中的Session

本文探讨了PortletSession在不同Portlet应用中的作用范围及其私有性,并介绍了如何在Portlet环境中获取全局HttpSession以访问CAS用户信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

PortletSession objects must be scoped at the portlet application context level. Each portlet application has its own distinct PortletSession object per user session. The portlet container must not share the PortletSession object or the attributes stored in it among different portlet applications or among different user sessions

在PortletAction中

PortletSession session = renderRequest.getPortletSession();
java.util.Map<String,Object> map = session.getAttributeMap();
//打印出所有的session
for(String s : map.keySet()){
System.out.println(s+":"+map.get(s));
}
String username = (String)session.getAttribute("user");
System.out.println(username);


再看

PortletSession session = renderRequest.getPortletSession();
java.util.Map<String,Object> map = session.getAttributeMap(PortletSession.APPLICATION_SCOPE);
//打印出所有的session
for(String s : map.keySet()){
System.out.println(s+":"+map.get(s));
}
String username = (String)session.getAttribute("user",PortletSession.APPLICATION_SCOPE);
System.out.println(username);


上面2个 唯一区别是:获取属性的方法多了PortletSession.APPLICATION_SCOPE。每个Portlet Application应该在Process Action和Render的时候应该拥有自己私有的Session对象,来为当前用户服务。也就是说,如果某一个用户在一个PORTAL系统中操作不同的PORTLET,而且这些PORTLET属于不同的PORTLET APPLICATION,则,这个用户将操作多个SESSION对象,用户在每个PORTLET中操作SESSION时,这个SESSION都将是属于该PORTLET所在的上下文的私有SESSION 对象。

如果我要获得全局的用户session,怎么办?比如要获取CAS的用户信息。CAS用户信息应该放在全局的HttpSession中,那LifeRay可进行如下操作:

//获取HttpSession,PortalUtil 在 portal-service下的
//com.liferay.portal.util.PortalUtil
HttpSession session = PortalUtil.getHttpServletRequest(renderRequest)
.getSession();
String username = (String) session
.getAttribute("edu.yale.its.tp.cas.client.filter.user");


可以看看PortalUtil中是怎么实现获取全局Session

//PortalUtil 中的实现方法
return getPortal().getHttpServletRequest(portletRequest);
//getPortal返回的Portal的实现类
//在portal-impL下的com.liferay.portal.util.PortalImpl
//它的实现
if (portletRequest instanceof PortletRequestImpl) {
PortletRequestImpl portletRequestImpl =
(PortletRequestImpl)portletRequest;

return portletRequestImpl.getHttpServletRequest();
}
else if (portletRequest instanceof PortletRequestWrapper) {
PortletRequestWrapper portletRequestWrapper =
(PortletRequestWrapper)portletRequest;

return getHttpServletRequest(portletRequestWrapper.getRequest());
}

HttpServletRequest request = HttpServletUtil.getHttpServletRequest(
portletRequest);

if (request != null) {
return request;
}

throw new RuntimeException(
"Unable to get the HTTP servlet request from " +
portletRequest.getClass().getName());
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值