LAST_PATH Redirection in Custom Login Post Action in Liferay 7

纵然是瞬间 提交于 2019-12-13 03:42:11

问题


I'm implementing custom login in Liferay 7 - one module for login hook portlet and one for ActionCommand.

I also generated class for post login event. The problem is redirection won't work on either this class or on JSP of login hook porlet.

This is my code for LifecycleAction:

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

    private static final Log LOGGER = LogFactoryUtil.getLog(UserLoginActionCommand.class);

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

        HttpServletRequest request = lifecycleEvent.getRequest();
        HttpServletResponse response = lifecycleEvent.getResponse();
        HttpSession session = request.getSession();

        try {
            User currentUser = PortalUtil.getUser(request);
            LOGGER.info("USER|" + currentUser.getFirstName() + "|" + currentUser.getGroup().toString());
            LOGGER.info("LASTPATH|" + session.getAttribute("LAST_PATH"));
            LastPath lastPath = (LastPath) session.getAttribute("LAST_PATH");
            LOGGER.info(lastPath.getPath());
            session.setAttribute(WebKeys.LAST_PATH, lastPath);
            response.sendRedirect(lastPath.getPath());
        } catch (PortalException | IOException exception) {
            LOGGER.error(exception);
        }
    }

}

This is my code for porlet login JSP:

<c:choose>
    <c:when test="<%= themeDisplay.isSignedIn() %>">        
        <%
        response.sendRedirect(WebKeys.LAST_PATH);
        %>
    </c:when>
    <c:otherwise>
...

My LOGGER is able to display correct values on console.

Thank you.


回答1:


The forwarding after login is a bit tricky.

If you use the LAST_PATH and session attributes you don't need the send redirect line because that will be handled by Liferay. The last path can be though affected by URL parameters as 'redirect'.

If you are going to use the send redirect approach. Build your URL and then past the URL that you created to the sendRedirect() call. Do not use the session parameter.

PS: If you are modifying the login have a look at the AuthPipeline rather the doing heavy overrides to the login portlet.



来源:https://stackoverflow.com/questions/47416572/last-path-redirection-in-custom-login-post-action-in-liferay-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!