« 返回到 Custom Post-login...

Customizing the default page after login

How do I specify which page a newly logged in user gets redirected to?

How do I redirect my users to the page they were just looking at.. after they login? #

All you need to do is set the following value in your portal-ext.properties file:

auth.forward.by.last.path=true

Also make sure that there is no landing page defined. Remove lines with

default.landing.page.path

How do I redirect my users to a specific page (or community).. after they login? #

4.3.x and on #

If your "redirect rule" is very simple.. such as "after login, redirect all users to page '/web/guest/home'", then you just need to add 2 values to your properties file:

auth.forward.by.last.path=true
default.landing.page.path=/web/guest/home

If you want to include more complex logic, you will need to modify the DefaultLandingPageAction.java file

DefaultLandingPageAction.java #

In portal.properties, notice the property login.events.post, after LoginPostAction is called, DefaultLandingPageAction is also called.

    login.events.post=com.liferay.portal.events.LoginPostAction,com.liferay.portal.events.DefaultLandingPageAction

This class "DefaultLandingPageAction.java" has been created for you here:

/portal-impl/src/com/liferay/portal/events/DefaultLandingPageAction.java

Liferay is setup to read two properties, AUTH_FORWARD_BY_LAST_PATH and DEFAULT_LANDING_PAGE_PATH. To do more complex redirect logic, we are basically setting a property to tell Liferay to send the newly logged in user to the page specified in path, the page he was just looking at. BUT.. before we do this, we override the value of path. Now we have complete control over where to send the user.

For more complex redirect logic we just need to pull the necessary values to get our desired path, then override the path variable.

You can look at this code snippet to help get you started on how to pull the desired information:

		ThemeDisplay themeDisplay =
			(ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);

		User user = themeDisplay.getUser();

		long userId = themeDisplay.getUserId();

		List userGroups = UserGroupLocalServiceUtil.getUserUserGroups(userId);

Finally, override this path variable

		String path = PropsUtil.get(PropsUtil.DEFAULT_LANDING_PAGE_PATH);

4.2.x and before #

take a look at the bottom of the LoginPostAction.java file. at the bottom of the run method is some code that has been commented out:

portal-ejb\src\com\liferay\portal\events\LoginPostAction.java:

         // 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"});

         LastPath lastPath = new LastPath("/c", "/portal/layout", params);

         ses.setAttribute(WebKeys.LAST_PATH, lastPath);*/

If you switch the "auth.forward.by.last.path" variable to "true", then after you login, it will redirect you to the url you were just at before you logged in.. which is stored in the variable "lastPath".

Soo.. lets say that you wanted to redirect certain users to certain pages. If you changed that "lastPath" value.. you now control what page the newly logged in user would get redirected to. You could get the logged in user info via:

PortalUtil.getUserId(req)

and then query the database based on that userid to get other info.

On a side note, if you were to do this quickly, you can just uncomment the code and add your own logic to set the lastPath variable at the end of the LoginPostAction method, but a cleaner way would be to put these modifications elsewhere.

1. create a new class, and in the run() method, put in your code. This class will have the same structure as LoginPostAction, except that it only contains the new code (the code that was commented out).

2. Then change the value in "portal.properties" by editting your "portal-ext.properties" file so that it will also hit your newly created class.

3. Add your own class to the "login.events.post" line so that it looks something like:

login.events.post=com.liferay.portal.events.LoginPostAction,yourclasshere
0 附件
140702 查看
平均 (1 投票)
满分为 5,平均得分为 2.0。
评论
讨论主题回复 作者 日期
Test for... Arcko Duan 2008年7月22日 下午7:39
I'm on 5.1.1 and this: ... Bob Evans 2008年9月27日 下午1:12
For me it works fine. You have to set the... Paul Voors 2008年9月30日 上午3:36
How do you set the friendly url? I am having... Michael Poznecki 2009年1月29日 上午7:05
Hi Michael, I'm looking for the same... please... suresh reddy k 2009年1月29日 下午1:32
hi, go to manage pages select the page for... Sachin Pathare 2009年4月23日 上午5:58
Michael, From the 'Communities' admin portlet... Jason Reast-Jones 2009年2月2日 上午7:50
Its working fine.. Thanks Ravi Kumar Gupta 2009年7月1日 上午3:17
We have implemented a CustomLandingPageAction... Edgar Vonk 2009年9月24日 上午6:17
Did you solve this issue? I have a similar... Andreas Thiel 2009年11月5日 上午7:04
I worked a bit on it. I injected my own... Andreas Thiel 2009年11月6日 上午3:34
Interesting! No, I never found a solution for... Edgar Vonk 2009年11月18日 上午4:23
Two gotchas I found when implementing my own... Peter Connolly 2009年9月25日 上午11:52
I have now been trying to make my own... Armaz Mellati 2011年1月9日 上午4:02
Now I got it working. It was kind of my own... Armaz Mellati 2011年1月9日 下午12:29
I have the same problem,I need to get the user... mohamed bouker 2011年4月28日 上午9:53
I m doing the same thing using hook ,but... Nitesh Kr Sahay 2012年1月10日 上午4:13

Test for http://support.liferay.com/browse/LEP-6816
在 08-7-22 下午7:39 发帖。
I'm on 5.1.1 and this:

auth.forward.by.last.path=true
default.landing.page.path=/web/guest/home

is simply not working.

I set default.landing.page.path=/web/10601/home

and it ALWAYS goes to /group/10601/1.

This is holding up our release to production. HELP PLEASE.
在 08-9-27 下午1:12 发帖。
For me it works fine.
You have to set the friendly url for the home page /home where your refer to in: "/web/10601/home".
(you can set an friendly url for "10601" to!)
在 08-9-30 上午3:36 发帖以回复 Bob Evans
How do you set the friendly url? I am having the same problem that Bob did.
在 09-1-29 上午7:05 发帖以回复 Paul Voors
Hi Michael,

I'm looking for the same... please let me know if you get pointers!!!

Suresh
在 09-1-29 下午1:32 发帖以回复 Michael Poznecki
Michael,

From the 'Communities' admin portlet either search for your community or click 'All Communities'. On the action menu for the community in question go to 'Manage Pages' Then click the 'Settings' tab, then 'Virtual Host', it's a bit misleading, but that's where you set the Friendly URL.

Jason
在 09-2-2 上午7:50 发帖以回复 Michael Poznecki
hi,
go to manage pages select the page for which u want, there is a option for friendly URL, change it accordingly

Sachin
在 09-4-23 上午5:58 发帖以回复 suresh reddy k
在 09-7-1 上午3:17 发帖。
We have implemented a CustomLandingPageAction using the description above and this works just fine. We have separate landing pages depending on the role of the user.

However I would also like to be able to redirect users to their landing page whenever they go the root URL of our portal ('/'). The functionality we have now is:
# user logs in
# Liferay redirects user to correct landing page (using our CustomLandingPageAction). e.g.: http://localhost:8080/group/test/home
# user manually changes the URL or follows a bookmark to: http://localhost:8080
# Liferay redirects the user to the _default_ landing page: http://localhost:8080/web/guest (or whatever URL is configured using the default.landing.page.path)

Is there an event or filter somewhere in Liferay that we can use to send users to their landing page when they go to the root URL?
在 09-9-24 上午6:17 发帖。
Two gotchas I found when implementing my own landing page forward logic:

1. The code for getting the user info returns null on the line:
ThemeDisplay themeDisplay = (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);

I had to get the user information this way instead:

String userIdString = request.getUserPrincipal().getName();
long userId = new Long(userIdString);
User user = UserLocalServiceUtil.getUser(userId);

2. The instructions don't talk about how to deploy the modified DefaultLandingPageAction. You could open portal-impl.jar and replace DefaultLandingPageAction.class. I'm not a big fan of modifying vendor jars. I would recommend opening portal-ext.properties and adding a line like this:

login.events.post=com.liferay.portal.events.LoginPostAction,com.liferay.por­tal.events.MyLandingPageAction

Create a class named MyLandingPageAction (you can look at the source for DefaultLandingPageAction for a sample) and modify accordingly. Jar it up by itself and then drop the jar in tomcat-6.0.18\webapps\ROOT\WEB-INF\lib
在 09-9-25 上午11:52 发帖。
Did you solve this issue? I have a similar demand: The company logo, normally placed in the top left, refers to ThemeDisplay.getURLHome(), which is _not_ the page my custom landing page dispatcher selects on login. How to achieve this?

Currently, I'm thinking about patching init.vm in the theme - this is where $company_url is set -; or to overwrite ThemeDisplay.setURLHome() - I have no clue what side effects this could cause.

Anybody?
在 09-11-5 上午7:04 发帖以回复 Edgar Vonk
I worked a bit on it.

I injected my own PortalUtil class, replacing PortalImpl, by adding the following lines to ext-spring.xml:

<bean id="com.liferay.portal.util.PortalUtil" class="com.liferay.portal.util.PortalUtil">
<property name="portal">
<bean class="com.vodafone.portal.liferay.MyPortalImpl" />
</property>
</bean>

MyPortalImpl inherits from PortalImpl and overrides the getHomeURL() method:

public class MyPortalImpl extends PortalImpl
{

@Override
public String getHomeURL(HttpServletRequest request) throws PortalException, SystemException
{
String portalURL = getPortalURL(request);
Company company = getCompany(request);
String homeURL = company.getHomeURL();
if (Validator.isNull(homeURL))
{
homeURL = PropsValues.COMPANY_DEFAULT_HOME_URL;
}

// added
HttpSession session = request.getSession(false);
if (session != null)
{
LastPath lp = (LastPath) session.getAttribute("LANDING_PAGE");
if (lp != null)
{
String specificHomeURL = lp.getPath();
if (Validator.isNotNull(specificHomeURL))
{
homeURL = specificHomeURL;
}
}
}
// end

return portalURL + getPathContext() + homeURL;
}
}

Works like a charm - unitl now ;-)

Could someone validate this approach? Is it feasible? Any sideeffects I did not encounter yet? Would it make sense to extend the "home URL" concept/strategy of Liferay in this regard (user specific home page takes precedence over company home page, takes precedence over default company home page)?
在 09-11-6 上午3:34 发帖以回复 Andreas Thiel
Interesting! No, I never found a solution for our problem. Quite bizarre because it seems to me almost everybody would have this issue.

Anyway, your solution sounds pretty good. How did you package this stuff? Where is the ext-spring.xml you are talking about?
在 09-11-18 上午4:23 发帖以回复 Andreas Thiel
I have now been trying to make my own DefaultLandingPageAction to work for several hours, but it just doesn't matter what I set the path to. No matter the value of the path set to the session by this code, it has no effect. I still get redirected to the 'guest' community.

LastPath lastPath = new LastPath(
StringPool.BLANK,
path,
new HashMap<String, String[]>());
HttpSession session = request.getSession();
session.setAttribute(WebKeys.LAST_PATH, lastPath);

I am starting to think I am facing a bug in 6.0.5.

Has anybody got this working on 6.0.5 ?

Regards,
Armaz (in very bad mood)
在 11-1-9 上午4:02 发帖。
Now I got it working. It was kind of my own fault.
I missed this one :
auth.forward.by.last.path=true
I had it set to false.

Now that I got here, I can summarize my experience :
- first I had a lot of problems making it work as an ext-plugin. I just had to give up, since the classloader just ignored my class and used the original one. I had to do it as Peter suggested. I made my own class and put it beside the original one in the source-code of the LR-portal. So I made a jar of the resulting Class file and put it in the lib directory under ROOT.

- So I got to the other problem that everyone else have pointed out, there is no WebKeys.THEME_DISPLAY in the session emoticon. Very irritating since I planed to use it in a call like this
group.getPathFriendlyURL(privateLayout, themeDisplay)
My workaround is hardcoding that part of the path ("/group").

- My last problem was the one I started this post with.

Good luck to other who are to walk this path.

Regards,
Armaz
在 11-1-9 下午12:29 发帖以回复 Armaz Mellati
I have the same problem,I need to get the user 's custum attribute by doing :
User us=PortalUtil.getUser(request);
String default =us.getExpandoBridge().getAttribute("nameofattr");
string path="/web/"+default;
but i can't get the expandobridge from session can you help me
在 11-4-28 上午9:53 发帖以回复 Armaz Mellati
I m doing the same thing using hook ,but classloader ignored my classes...
what can i do for this?........
在 12-1-10 上午4:13 发帖以回复 mohamed bouker