问题
I have a problem trying to fire up an autologin class in Liferay CE 6.1.1 when performing a redirection from a custom action method. Let me explain the scenarios.
Functional scenario 1: direct POST
action
Let's say I have a public page A with a login form (with username and password), an autologin class to perform some checks and a private page B to show some data after proper authentication.
I have a JSP with the login form in a portlet deployed in public page A, as follows (this is an oversimplified version):
<form method="post" action="https://localhost:4444/path/to/private/page/b">
<input id="username" type="text" />
<input id="password" type="password" />
<input type="submit" />
</form>
When the submit button is pressed, the autologin class is fired up and, if the checks are passed, the user lands in the private page B, as intended.
Non-functional scenario #2: redirection through custom action
The previous scenario doesn't work in certain situations where a previous authentication has been performed, so I need to add some checks before the autologin gets executed.
I thought that creating a custom action linked to the form was a good idea, so I rewrote the form as follows:
<portlet:actionUrl name="actionLogin" var="urlActionLogin" />
<form method="post" action="${urlActionLogin}">
<input id="username" type="text" />
<input id="password" type="password" />
<input type="submit" />
</form>
In the main class of the portlet deployed in public page A, we need a method to deal with the form action (oversimplifying again):
public void actionLogin (ActionRequest request, ActionResponse response) {
if (testsArePassed ( )) {
response.sendRedirect ("https://localhost:4444/path/to/private/page/b");
} else {
// Error page.
}
}
Putting aside the passing of parameters (I used copy-request-parameters
in portlet.xml
to ensure that), all I get is the same public page A again, with no autologin and with an URL like:
https://localhost:4444/path/to/public/page/a?p_p_id=58&p_p_lifecycle=0&_58_redirect=%2Fpath%2Fto%2Fprivate%2Fpage%2Fb
I guess I'm making some kind of conceptual mistake here, but, what's the reason why the autologin is not fired up in the second scenario, allowing navigation to private page B?
Thanks!
回答1:
Assuming you are using Generic Portlet.
After every processAction doView method is called before rendering the jsp page. So handle it this way. Goto Liferay Forum for more details.
public class MedelogGetAdmin extends GenericPortlet {
String actionJsp = "";
public void processAction(ActionRequest request, ActionResponse response) throws PortletException, IOException {
//from variables
String formType = request.getParameter("formType");
String buttonClicked = request.getParameter("button");
//check what page to forward to
actionJsp = Action.forwardTo(formType, buttonClicked); //jsp = consumers.jsp
//forward to "/WEB-INF/jsp/"+jsp;
//but how?
}
public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException {
response.setContentType("text/html");
if(actionJsp == null || actionJsp.equals("")){
PortletRequestDispatcher dispatcher =
getPortletContext().getRequestDispatcher("/WEB-INF/jsp/MedelogGetAdmin_view.jsp");
dispatcher.include(request, response);
}else{
PortletRequestDispatcher dispatcher =
getPortletContext().getRequestDispatcher("/WEB-INF/jsp/"+actionJsp);
dispatcher.include(request, response);
}
//set the actionJsp back to default
actionJsp = "";
}
}
来源:https://stackoverflow.com/questions/33606682/autologin-firing-in-liferay-from-a-redirection