0

I have a custom post login event class that in Liferay DXP. I am trying to handle exceptions by doing a redirect to an external (non-Liferay) error page.

The code I have in place is not redirecting.

I have tried to do a redirect with the lifecycleEvent.getResponse().sendRedirect(redirectURL); and request.setAttribute(AutoLogin.AUTO_LOGIN_REDIRECT, redirectURL);. Both of these don't work in this class below.

I am not sure if the post login lifecycle action has different commands for the request.setattribute to work or that if it is too late lifecycleEvent.getResponse() to work because the response is already committed.

There are no errors in the logs or otherwise. The user simply is directed to the Liferay default landing page after login whether there are exceptions or not in post login.

Has anyone tried to get this type of error handling to work in Liferay DXP? Any insight is much appreciated. I am under a very tight deadline to get this to work.

My code below:

@Component ( immediate = true, property = { "key=login.events.post" }, service = LifecycleAction.class )

public class CustomPostLoginFilter implements LifecycleAction {

@Override
public void processLifecycleEvent(LifecycleEvent lifecycleEvent) throws ActionException {

    final HttpSession session = lifecycleEvent.getRequest().getSession();
    HttpServletRequest request = lifecycleEvent.getRequest();

    String redirectURL = loginKeys.defaultErrorRedirectURL; 

    try {   
        // custom code
    } catch(PortalException e) {
        _log.error("Error in post login filter portal exception "  + redirectURL, e);
        request.setAttribute(AutoLogin.AUTO_LOGIN_REDIRECT, redirectURL);
    } catch (Exception e) {
        _log.error("Error in post login filter exception "  + redirectURL, e);      

        try {
            _log.info("Trying to post login filter redirect ");     
            if (Validator.isNotNull(redirectURL)) {
                _log.info("redirect url not null ");    
                lifecycleEvent.getResponse().sendRedirect(redirectURL);
            }
            else {
                _log.info("redirect url null"); 
            }
        } catch (Exception e1) {
            _log.error("Error in post login filter redirect ", e1);     

        }
        // does not seem to redirect on post login
        //request.setAttribute(AutoLogin.AUTO_LOGIN_REDIRECT, redirectURL);
    } 
}

}

Thanks!

VC1
  • 1,660
  • 4
  • 25
  • 42

1 Answers1

2

We also do a custom redirect after login to a certain Landing Page but use the LAST_PATH session attribute here:

@Override
public void processLifecycleEvent(LifecycleEvent lifecycleEvent) throws ActionException {
    HttpSession ses = lifecycleEvent.getRequest().getSession();
    LastPath lastPath = new LastPath("/group/your-landing-page", "", "");
    ses.setAttribute(WebKeys.LAST_PATH, lastPath);
}

This snippet worked on our DXP environment.

Andre Albert
  • 1,386
  • 8
  • 17
  • Also, there is a discussion about this on [last-path-redirection-in-custom-login-post-action-in-liferay-7](https://stackoverflow.com/questions/47416572/last-path-redirection-in-custom-login-post-action-in-liferay-7) – Andre Albert Jul 12 '18 at 09:42
  • Thanks Andre! I will try it. – VC1 Jul 12 '18 at 13:33
  • Hi Andre, I tried the last path code above. This does not work with an EXTERNAL URL. It adds the lastpath url to the Portal host url as if it is a relative url redirect. I would like to redirect to the http://example1.com from the portal url http://example.com on errors. I tried using session.setAttribute(WebKeys.REDIRECT, redirectURL). This did not work at all either – VC1 Jul 12 '18 at 14:49
  • In a simple/naive approach, could you just implement a Liferay Portal Delegate Servlet that just redirects browser to a certain (yet configurable) URL? Then use the LAST_PATH of "/delegate/example1" in your processLifecycleEvent. But may be there is better fitting solution – Andre Albert Jul 12 '18 at 15:14
  • see [usage-of-portaldelegateservlet-in-liferay](https://stackoverflow.com/questions/26753468/usage-of-portaldelegateservlet-in-liferay) - `servlet-class` is a plain http servlet – Andre Albert Jul 12 '18 at 15:24
  • Thanks is there no other way to do this other than a delegate portlet? – VC1 Jul 12 '18 at 17:57
  • Redirect should normally take place during process Action phase, so i guess you need to get a ActionRequest/ActionResponse based API. Maybe a Login [liferay-dxp-actioncommand-hook](http://www.javasavvy.com/liferay-dxp-actioncommand-hook/) will help. Extending/wrapping the LoginAction maybe. – Andre Albert Jul 12 '18 at 19:32
  • Thanks. I used last path to redirect to and internal error page (/web/guest/error) which is a redirect to the external URL. Much simpler workaround than all the invasive platform code hooks needed. I don't like the Klugey approach but not sure of a simpler code way to do it yet – VC1 Jul 12 '18 at 21:12