掲示板

Regarding redirecting to a page before login and after login

8年前 に subhash Peddyreddy によって更新されました。

Regarding redirecting to a page before login and after login

New Member 投稿: 14 参加年月日: 15/09/21 最新の投稿
I have a scenario to be developed for my client saying.
1. Auto login happens if the user is present and a page of that user to be shown with some portlet on it.
2. If there is no user id mentioned or user is not active then i should redirect him to the page where a proper user message should be shown.
3. if the user is not present then we need to redirect the user to the registration page.

I wrote a auto login hook which looks like this


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.model.User;
import com.liferay.portal.security.auth.AutoLogin;
import com.liferay.portal.security.auth.AutoLoginException;
import com.liferay.portal.service.UserLocalServiceUtil;

public class AutoLoginAction implements AutoLogin{
	
	static Log logger = LogFactoryUtil.getLog(AutoLoginAction.class);

	@Override
	public String[] handleException(HttpServletRequest arg0,
			HttpServletResponse arg1, Exception arg2) throws AutoLoginException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String[] login(HttpServletRequest arg0, HttpServletResponse arg1)
			throws AutoLoginException {
		
		String userId = arg0.getHeader("userId");
		
		try {
			logger.debug("*************** Auto Login Action ***************");
			User user = UserLocalServiceUtil.getUser(new Long(userId));
			if(Validator.isNotNull(user)){
				logger.info("user ="+user.toString());
				
				return new String[]{ String.valueOf(user.getUserId()), user.getPassword(), String.valueOf(user.getPasswordEncrypted()) };
			}else{
				arg0.setAttribute(AutoLogin.AUTO_LOGIN_REDIRECT, arg0.getContextPath()+"/web/guest/register");
			}
		} catch (NumberFormatException e) {
			arg0.setAttribute(AutoLogin.AUTO_LOGIN_REDIRECT, arg0.getContextPath()+"/web/guest/register");
			e.printStackTrace();
		} catch (PortalException e) {
			arg0.setAttribute(AutoLogin.AUTO_LOGIN_REDIRECT, arg0.getContextPath()+"/web/guest/register");
			e.printStackTrace();
		} catch (SystemException e) {
			arg0.setAttribute(AutoLogin.AUTO_LOGIN_REDIRECT, arg0.getContextPath()+"/web/guest/register");
			e.printStackTrace();
		}		
		return null;
	}

}


my portal.properties file looks like this


auto.login.hooks=com.dictu.kepp.autologin.AutoLoginAction
login.events.pre=com.dictu.kepp.autologin.PreLoginPageAction
login.events.post=com.dictu.kepp.autologin.PostLoginLandingPageAction

auto.login.ignore.paths= /web/guest/register

override.default.landing.page.path=true
auth.forward.by.last.path=true
auth.forward.by.redirect=true


my landing page action looks like this


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.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.struts.LastPath;
import com.liferay.portal.kernel.util.PrefsPropsUtil;
import com.liferay.portal.kernel.util.PropsKeys;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.model.User;
import com.liferay.portal.util.PortalUtil;

/**
 * @author peddyrsu
 *
 */
public class PostLoginLandingPageAction extends Action{
	
	static Log logger = LogFactoryUtil.getLog(PostLoginLandingPageAction.class);

	@Override
	public void run(final HttpServletRequest request, final HttpServletResponse response)
			throws ActionException
	{
		try
		{
			doRun(request, response);
		} catch (Exception e)
		{
			throw new ActionException(e);
		}
	}

	protected void doRun(final HttpServletRequest request, final HttpServletResponse response)
			throws SystemException, PortalException
	{
		
		long companyId = PortalUtil.getCompanyId(request);

		String path = PrefsPropsUtil.getString(companyId, PropsKeys.DEFAULT_LANDING_PAGE_PATH);

		// Check for override.default.landing.page.path property value
		boolean overrideDefaultLandingPagePath = PrefsPropsUtil.getBoolean(companyId, "override.default.landing.page.path");

		if (overrideDefaultLandingPagePath)
		{
			path = getCustomLandingPage(request);

		}

		if (Validator.isNotNull(path))
		{
			HttpSession session = request.getSession();
			session.setAttribute(WebKeys.LAST_PATH, new LastPath(StringPool.BLANK, path));
		}
	}

	/**
	 * Returns custom landing page path after user login
	 * 
	 * @param request
	 * @return
	 * @throws PortalException
	 * @throws SystemException
	 */
	private String getCustomLandingPage(final HttpServletRequest request) throws PortalException,
			SystemException
	{
		User user = PortalUtil.getUser(request);
		StringBuilder sb = new StringBuilder();
		sb.append("/web/"+user.getScreenName()+"/home");
		return sb.toString();
	}

}


where as register page is a public page.

I tried all the possible ways but i am not getting any, can any one of suggest/correct me please where am i going wrong, or completely wrong.

this is my third post where am writing for help.

Thanks & Regards
Subhash Reddy P
thumbnail
8年前 に Andrew Jardine によって更新されました。

RE: Regarding redirecting to a page before login and after login

Liferay Legend 投稿: 2416 参加年月日: 10/12/22 最新の投稿
Hi Subhash,

I'll try to help you out on this one -- first I need to make sure I understand your scenario because your explanation was a little broken. Questions first.

1. Do you have public and private pages?

2. When a user authenticates, are they authenticating in Liferay? or with an external system?

3. If the objective to detect a logged in user and to take them to the "user homepage" rather than the default landing page?

If you are logging in using Liferay, and you are trying to take a logged in user to their homepage, then you might just need to alter your default landing page path. This is in the portal.properties that we can override in the portal-ext.


##
## Default Landing Page
##

    #
    # Set the default landing page path for logged in users relative to the
    # server path. This is the page users are automatically redirected to after
    # logging in. For example, if you want the default landing page to be
    # http://localhost:8080/web/guest/login, set this to /web/guest/login. To
    # activate this feature, set auth.forward.by.last.path to true. To customize
    # the behavior, see com.liferay.portal.events.DefaultLandingPageAction in
    # the "login.events.post" property above.
    #
    # The following variables can be used: ${liferay:screenName} and
    # ${liferay:userId}.
    #
    default.landing.page.path=
    #default.landing.page.path=/web/guest/login
    #default.landing.page.path=/user/${liferay:screenName}/home
8年前 に subhash Peddyreddy によって更新されました。

RE: Regarding redirecting to a page before login and after login

New Member 投稿: 14 参加年月日: 15/09/21 最新の投稿
Hi Andrew,
thank you very much for reply to help me out.
1. Do you have public and private pages?
Yes i should have 1 public page for showing all the user validation messages if he is not a valid one before login.
1 private page where he will have a different portlet of his own to display his things after login.

2. When a user authenticates, are they authenticating in Liferay? or with an external system?
the user will be registered in liferay database for the first time when he is getting registering in the external system.
Yes first we check the user in the external system, if the user is present there we will load the information from liferay database and then auto login him.
For us the user validation is all done with the reference to external system using the soap service calls.

as i am new am unable to understand the flow clearly with the examples.
It would be great of you if you help me in this, please.

Thanks & regards
Subhash reddy p
thumbnail
8年前 に Andrew Jardine によって更新されました。

RE: Regarding redirecting to a page before login and after login

Liferay Legend 投稿: 2416 参加年月日: 10/12/22 最新の投稿
Hi Subhash,

Ok -- I have done something similar in the past so I can probably help you out on this one. In my case I did it using an external system using a REST call, but the workflow was almost the same. Here is what we did.

1. User goes to the Liferay Login Page. (we made a JSP hook to alter the presentation of this page).
2. User enters their credentials and submits.
3. We created a StrutsAction hook to override the /login/login action. In this hook we call the external service to validate the login information.
4. If the user was not valid, then we send them to the registration page -- if the are we sent them to the correct post login location. In our case, the post login location was based on the "LAST_PATH" in case the login was triggered from somewhere deep in the public site. In your case, I think you just sent them to the default landing page which you configure to be the users personal page?

I'll have to dig out the code as it was a few years ago, but if that sounds right, let me know and I'll see if I can find it.
8年前 に subhash Peddyreddy によって更新されました。

RE: Regarding redirecting to a page before login and after login

New Member 投稿: 14 参加年月日: 15/09/21 最新の投稿
HI Andrew,
Thank you very much for the help and sorry for the delay in responding back but the solution worked as i did mostly similar in the way you said me.
I am validating and if the user is fine then auto-logging him inside and redirecting him to the private page of my site else i am redirecting him to the public page where i have registration portlet.

I have deployed it in Acceptance also this flow is working fine. But recently i got an issue saying the liferay part is not allowing the user to login using admin credentials. My implementation goes like this
1. I have a auto-login hook and post login hook.
2. I have a public page named welcome in my site and i have added a sign in portlet in that.
3 my properties file looks like this.
portal-ext.properties
------------------------------
#Liferay property for setting the theme's shorcut/favicon icon.
theme.shortcut.icon=ro-favicon-wit-0xffffff.png

#Liferay property for accepting the https requests
#web.server.protocol=https

#The default home URL of the portal.
virtual.hosts.default.site.name=mysite

#Set the following to true if the portal should show HTTP status codes like 404 if the requested page is not found.
layout.show.http.status=true
layout.friendly.url.page.not.found=/web/mysite/register

#Liferay settings for ignoring the redirected path from blocking
auto.login.ignore.paths=/web/mysite/register

#Liferay public pages
mysite.public.welcome.page = /web/mysite/welcome
mysite.public.register.page = /web/mysite/register

#Removing all the session values stored in the servlet cache
com.liferay.portal.servlet.filters.cache.CacheFilter=false
com.liferay.portal.servlet.filters.etag.ETagFilter=false
com.liferay.portal.servlet.filters.header.HeaderFilter=false

#Set this to true if you want the portal to force the browser cache to be disabled.
#It will only disable the cache for the rendered HTML response It will not have an impact
#on static content or other resources.
browser.cache.disabled=true

#Set this true if you want to disable the cache for authenticated users.
#This property is not read when the property "browser.cache.signed.in.disabled" is true.
#This is useful to ensure that authenticated users cannot go to the sign in page by clicking on the back
#button in their browsers.
browser.cache.signed.in.disabled=true

#Remove the session id in the url
session.enable.url.with.session.id=false

#Session time setting for expiry
session.timeout=30
session.timeout.auto.extend=false
session.timeout.redirect.on.expire=true
default.logout.page.path=/

Now i will explain my issue.

I have applied my custom theme to the welcome page where sign in portlet is present. When i am trying to login as admin user from that page it is not working in Test and Acceptance Environment but it is working fine my development environment where in i can see in log it is entering into the autologin and as it doesn't have any headers i return empty string array from auto login hook.

I am unable to understand what is wrong in this.

Thanks & Regards
Subhash Reddy P
3年前 に Ram Sunka によって更新されました。

RE: Regarding redirecting to a page before login and after login

New Member 投稿: 5 参加年月日: 18/04/10 最新の投稿
Hello,
am using liferay(6.1.2 CE GA3) custom login feature.
1. When i hit the localhost:8080/en?USER_ID=Ram,
2. request is coming to login() method of LiferayAutoLogin class which  implements AutoLogin interface.
3. And then request forwarded to  login.events.post=com.xxx.xxx.Abc.   
4. In Abc class, PortalUtil.getCurrentURL(request) is giving "/c". i was expecting "en?USER_ID=Ram".
can someone help me here.