Custom redirect after login liferay
来源:互联网 发布:金刚石晶体结构数据 编辑:程序博客网 时间:2024/05/03 00:17
I assume you installed liferay portal v5.2.3 Bundled with Tomcat 5.5 by this way http://blog.csdn.net/wxhawk/archive/2010/11/15/6009959.aspx. Our target is to redirect to a specified community page after a user login. Of course the user is a member of the specified community.
Another target is not to change liferay source code, just to override its default behavior.
go to http://sourceforge.net/projects/lportal/ and download liferay-portal-src-5.2.3.zip,
unzip the downloaded source code and import to eclipse to create a new class CustomLoginPostAction in portal-impl sub-project in liferay, the code is here
package com.liferay.portal.events;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.liferay.portal.kernel.events.Action;
import com.liferay.portal.kernel.events.ActionException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.model.Group;
import com.liferay.portal.service.GroupLocalServiceUtil;
import com.liferay.portal.struts.LastPath;
import com.liferay.portal.util.WebKeys;
import java.util.List;
public class CustomLoginPostAction extends Action
{
private static Log _log = LogFactoryUtil.getLog(CustomLoginPostAction.class);
public void run( HttpServletRequest req, HttpServletResponse res ) throws ActionException
{
try
{
HttpSession ses = req.getSession();
Map params = new HashMap();
Long id = ( Long ) ses.getAttribute( "USER_ID" );
long userId = id.longValue();
long groupId = 0;
String frindlyUrl = null;
List groups = GroupLocalServiceUtil.getUserGroups( userId );
for ( Group aGroup : groups )
{
if( aGroup.isCommunity() && !aGroup.getName().equals( "Guest" ) )
{
groupId = aGroup.getGroupId();
frindlyUrl= aGroup.getFriendlyURL();
}
}
LastPath lastPath = null;
if( groupId <= 0 )
{
lastPath = new LastPath( "/c", "/portal/layout", params );
}
else
{
lastPath = new LastPath( "", "/web"+frindlyUrl, params );
}
ses.setAttribute( WebKeys.LAST_PATH, lastPath );
System.out.println("User logined in , redirect to page:" + lastPath);
_log.info( "User logined in , redirect to page:" + lastPath );
} catch ( Exception e )
{
throw new ActionException( e );
}
}
}
copy CustomLoginPostAction.class to C:/liferay-portal-5.2.3/tomcat-5.5.27/webapps/ROOT/WEB-INF/classes/com/liferay/portal/events
if you cannot find folder com/liferay/portal/events under C:/liferay-portal-5.2.3/tomcat-5.5.27/webapps/ROOT/WEB-INF/classes, just create it manually.
add the two config items in port-ext.properties which location is C:/liferay-portal-5.2.3/tomcat-5.5.27/webapps/ROOT/WEB-INF/classes
auth.forward.by.last.path=true
login.events.post=com.liferay.portal.events.DefaultLandingPageAction,com.liferay.portal.events.CustomLoginPostAction
This setting will overwrite the value of "auth.forward.by.last.path" in portal.properties which default value is "false".
Now, after users login, they will be redirected to the last page they were at, instead of their own private community.
The second item means CustomLoginPostAction will replace DefaultLandingPageAction which is the default redirect processor.
---- how to test ---
1. create a community A and community B and design public page for these two communities.
2. create a new account usera for community A and account userb for community B.
3. if you login as usera, system will redirect to community A public page, not system welcome page instead.
if you login as userb, system will redirect to community B public page as well.
Sombody suggests to add above class file into portal-impl.jar, it is not a good practise because you change the original liferay.
if you reinstall liferay, you will lose the new redirect logic.
if want to redirect to user's personal page, CustomLoginPostAction should be following:
package com.liferay.portal.events;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.liferay.portal.kernel.events.Action;
import com.liferay.portal.kernel.events.ActionException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.model.Group;
import com.liferay.portal.service.GroupLocalServiceUtil;
import com.liferay.portal.struts.LastPath;
import com.liferay.portal.util.WebKeys;
import java.util.List;
import com.liferay.portal.model.User;
import com.liferay.portal.service.UserLocalServiceUtil;
public class CustomLoginPostAction extends Action
{
private static Log _log = LogFactoryUtil.getLog(CustomLoginPostAction.class);
public void run( HttpServletRequest req, HttpServletResponse res ) throws ActionException
{
try
{
HttpSession ses = req.getSession();
Map params = new HashMap();
// Long id = ( Long ) ses.getAttribute( "USER_ID" );
// long userId = id.longValue();
User loginUser = UserLocalServiceUtil.getUser(com.liferay.portal.util.PortalUtil.getUserId(req));
LastPath lastPath = null;
if( loginUser == null )
{
lastPath = new LastPath( "/c", "/portal/layout", params );
}
else
{
lastPath = new LastPath( "", "/web/"+loginUser.getScreenName(), params );
}
ses.setAttribute( WebKeys.LAST_PATH, lastPath );
System.out.println("User logined in , redirect to page >>>>>>> " + lastPath);
_log.info( "User logined in , redirect to page:" + lastPath );
} catch ( Exception e )
{
throw new ActionException( e );
}
}
}
- Custom redirect after login liferay
- Custom redirect after login
- Redirect to different pages after Login with Spring Security
- Custom redirect after creating a new Sharepoint Item
- How to redirect to login page after cookie expires in Angular JS?
- Redirect after Post
- Redirect After Post
- Liferay 4.2.2 login problem
- Liferay Custom fields 的使用方法
- liferay-portlet:login 登陆页面的修改
- liferay-portlet:login 登陆页面的简化
- liferay ide 创建主题 Custom Classic Theme with Liferay IDE
- [Liferay] liferay自定义查询语句custom sql实例
- Custom fields of Organization in Liferay
- return new ModelAndView("redirect:/login.jsp");
- Spring Security Custom Login Form Example
- Spring Security Custom Login Form Annotation Example
- Putty exits immediately after entering login command
- 软件人员推荐书目(一) 大师篇
- Java Applet 程序的远程调试方法
- C++强制类型转换
- group by cube
- 修复Ubuntu10.04上chrome中文输入法问题
- Custom redirect after login liferay
- java泛型map的用法
- C#邮件发送和接收
- 基于svnserve的服务器,权限文件authz配置的常见问题及解答
- Map Reduce – the Free Lunch is not over?
- SQL中distinct的用法 and 查询编号最大的字段
- 数字图像处理中常用的插值方法
- 使用并查集的Kruscal方法
- 关于DNS反解的一些资料