1

私のアプリケーションは struts 1 を使用しており、ページは struts-config.xml のアクション パスの属性「roles」を使用してロール保護されています (つまり、ロールで許可されていない場合、ユーザーはページにアクセスできません)。

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

この方法では、ユーザーがログインしていないか、「管理者」ロールを持っていない場合、保護されたページではなくホームページが表示されます。

さて、これはすべて完全に機能します。唯一の問題は、ブラウザーの URL (したがって、servlet_path の値) が「homepage.do」ではなく「ProtectedPageAction.do」であること、つまり、servlet_path が「in表示されているページと同期します。

servlet_path の値を操作する必要があるため、ユーザーがページを表示する権限がない場合、ブラウザに表示される URL は「ProtectedPageAction.do」ではなく「homepage.do」でなければなりません。これはセキュリティ上の理由でもあります。ユーザーが URL に「ProtectedPageAction.do」が含まれていることに気付いた場合、それは何のためにあるのか、どのようにアクセスすればよいのか疑問に思うかもしれません。

4

1 に答える 1

1

リダイレクトは通常、アクション転送属性 redirect="true" を設定することによって行われます。あなたの場合、アクションを作成する必要があります

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;
  }
}

または自然に構成を使用します

<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;
  }
}
于 2013-02-07T15:59:23.557 に答える