问题
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