Custom redirect after login

1.requirement

     We use liferay as portal container, our system support multi-company by difference community; we hope the page redirect the community's private page after login.

 

2.solution
     step.1

         create and edit  the liferay configuration file

                 portal-ext.properties     auth.forward.by.last.path=true

       copy this file to  ${liferay_home}/server/default/deploy/ROOT.war/WEB-INF/classes/ portal-ext.properties

       This will overwrite the "auth.forward.by.last.path" value in portal.properties which is set to "false" by default. Now, after users login, they will be redirected to the last page they were at, instead of their own private community.

 

      step.2  

         if we want to customize the redirect even, we need to add and edit one more  file:

               com.liferay.portal.events.LoginPostAction.java

         Show code:

            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.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 org.apache.log4j.Logger; /** * * @author Mark Xie */ public class CustomLoginPostAction extends Action { public static final Logger logger = Logger.getLogger(CustomLoginPostAction.class); public void run(HttpServletRequest req, HttpServletResponse res) throws ActionException { try { HttpSession ses = req.getSession(); // To manually set a path for the user to forward to, edit // portal.properties and set auth.forward.by.last.path to true. Map params = new HashMap(); //params.put("p_l_id", new String[] {"PRI.3.1"}); //params.put("p_l_id", new String[] {"PUB.1.1"}); Long id = (Long)ses.getAttribute("USER_ID"); long userId = id.longValue(); long groupId = 0; List groups = GroupLocalServiceUtil.getUserGroups(userId); for (Group aGroup : groups) { if (aGroup.isCommunity() && !aGroup.getName().equals("Guest")) { groupId = aGroup.getGroupId(); } } LastPath lastPath = null; if (groupId <= 0) { lastPath = new LastPath("/c", "/portal/layout", params); } else { lastPath = new LastPath("", "/group/"+groupId, params); } ses.setAttribute(WebKeys.LAST_PATH, lastPath); logger.info("User logined in , redirect to page:" + lastPath); } catch (Exception e) { throw new ActionException(e); } } } 

 

     compile the java file and copy the LoginPostAction.class to server/default/deploy/ROOT.war/WEB-INF/classes/com/liferay/portal/events/LoginPostAction.class

 

  step.3

      We will overwrite a couple values in "portal.properties" by editing our "portal-ext.properties" file. Add these 2 lines of code:
  auth.forward.by.last.path=true
 login.events.post=com.liferay.portal.events.LoginPostAction,com.liferay.portal.events.CustomLoginPostAction

你可能感兴趣的:(portal&portlet,redirect,login,hashmap,file,exception,string)