仅在Struts 1.x中将HTTP请求限制为"POST"

Jon*_*han 6 java struts http

Struts 1.x中是否存在可配置的方式,因此我的操作类仅在HTTP'POST'上执行.

我知道我可以request.getMethod()在我的动作类中使用,然后根据它做某些"东西".

此致,乔纳森

McD*_*ell 5

您可以使用您web.xml来定义访问权限.此约束可防止GET请求:

  <security-constraint>
    <web-resource-collection>
      <web-resource-name>struts action servlet</web-resource-name>
      <url-pattern>*.do</url-pattern>
      <http-method>GET</http-method>
    </web-resource-collection>
    <auth-constraint>
      <!-- no one! -->
    </auth-constraint>
  </security-constraint>
Run Code Online (Sandbox Code Playgroud)


Kev*_*son 3

这是一些编程和配置解决方案的想法。您可以创建自定义 ActionMapping...

public class YourPOSTRequiredActionMapping extends ActionMapping { }
Run Code Online (Sandbox Code Playgroud)

...并在 struts 配置中使用仅 POST 的映射。

<action path="/your/path" type="YourAction" className="YourPOSTRequiredActionMapping" />
Run Code Online (Sandbox Code Playgroud)

然后,您可以扩展 struts RequestProcessor 并覆盖 processMapping

public class YourRequestProcessor extends RequestProcessor {
    protected ActionMapping processMapping(HttpServletRequest request, HttpServletResponse response, String path) throws IOException {
        ActionMapping mapping = super.processMapping(request, response, path);
        if (mapping instanceof YourPOSTRequiredActionMapping) {
            if (!request.getMethod().equals("POST")) {
                mapping = null;
            }
        }
        return mapping;
    }
}
Run Code Online (Sandbox Code Playgroud)

确保配置您的 struts 配置以使用 YourRequestProcessor。

<controller processorClass="YourRequestProcessor" nocache="true" contentType="text/html; charset=UTF-8" locale="false" />
Run Code Online (Sandbox Code Playgroud)

我基于一些旧的工作代码,但我什至还没有编译上面的示例代码。