1

My application is using struts 1 and the pages are role-protected (i.e.: a user cannot access a page if his role doesn't allow him) using the attribute "roles" of the action path in the struts-config.xml:

<action path="/ProtectedPageAction" type="org.apache.struts.actions.ForwardAction"
    parameter="ProtectedPage" roles="admin" />

this way, if a user is either not logged in or doesn't have the role "admin", he sees the homepage instead of the protected page.

Now, all of this works perfectly, the only problem being that the URL in the browser (hence the value of servlet_path) is not "homepage.do" but "ProtectedPageAction.do" or, in other words, the servlet_path is not "in sync" with the shown page.

I need to work with the value of servlet_path hence when the user is not authorised to see a page, the url shown in the browser must be "homepage.do" and not "ProtectedPageAction.do"; this is also for security reason: if a user notices "ProtectedPageAction.do" in the URL might start wondering what's that for and how to access it etc.

Charles
  • 50,943
  • 13
  • 104
  • 142
Pierpaolo
  • 567
  • 3
  • 7
  • 17

1 Answers1

1

Redirects usually done via setting the action forward attribute redirect="true". In your case you need to create action

public class RedirectAction extends ForwardAction {
  @Override
  public ActionForward execute(
    ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request,
    HttpServletResponse response)
    throws Exception {

    // Create a RequestDispatcher the corresponding resource
    String path = mapping.getParameter();

    if (path == null) {
        throw new ServletException(messages.getMessage("forward.path"));
    }

    // Let the controller handle the request
    ActionForward retVal = new ActionForward(path, true);
    retVal.setContextRelative(true);

    return retVal;
  }
}

or naturally use the configuration

<action path="/ProtectedPageAction" type="org.yourname.struts.actions.ProtectedPageAction"
    parameter="ProtectedPage" roles="admin">
  <forward name="success" path="/Homepage.do" redirect="true"/>
</action>

public class ProtectedPageAction extends Action {
  @Override
  public ActionForward execute(
    ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request,
    HttpServletResponse response)
    throws Exception {

    // Let the controller handle the request
    ActionForward retVal = mapping.findForward("success");
    retVal.setContextRelative(true);

    return retVal;
  }
}
Roman C
  • 49,761
  • 33
  • 66
  • 176