【问题标题】:Curl POST request for spring mvc gives 405 Method 'POST' not supportedspring mvc 的 curl POST 请求给出了 405 Method 'POST' not supported
【发布时间】:2016-02-25 09:32:15
【问题描述】:

我有一个 SPRING 4 申请。它使用 Spring Security Ldap 身份验证进行身份验证。一切正常,但是当我尝试发出 POST 请求时,它给我一个 WARN 失败:org.springframework.web.servlet.PageNotFound - 不支持请求方法“POST”。

我正在尝试在我的脚本中使用 curl 命令执行发布请求,该命令触发控制器的 POST 方法并上传文件。

spring-security.xml

 <?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:security="http://www.springframework.org/schema/security"
    xsi:schemaLocation="http://www.springframework.org/schema/security
      http://www.springframework.org/schema/security/spring-security.xsd
      http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans.xsd">


    <security:http auto-config="true" use-expressions="true">
      <security:intercept-url pattern="/login" access="permitAll()" />




      <security:intercept-url pattern="/info"  access="permitAll()" method="GET" />
      <security:intercept-url pattern="/bookings" access="isAuthenticated()" method="POST"/>

      <security:intercept-url pattern="/chartdata"    access="isAuthenticated()" method="GET" />
      <security:intercept-url pattern="/uploadData/*" access="isAuthenticated()" method="POST"/> <!-- Change2 -->
  <security:intercept-url pattern="/**"     access="isAuthenticated()" />

      <security:form-login login-page="/login"
        login-processing-url="/performLogin"
        authentication-failure-url="/login?error=true" />
      <access-denied-handler error-page="/login" />
    </security:http>


    <security:authentication-manager alias="authenticationManager">
        <security:ldap-authentication-provider  user-dn-pattern="uid={0}, ou=People,o=XXX" />
    </security:authentication-manager>

    <ldap-server url="ldap://ldap.xxx.com/dc=xxx,dc=com" port="389" />
</beans:beans>

Controller.java

@RequestMapping(value = "/uploadData/{type}****?${_csrf.parameterName}=${_csrf.token}****", headers = {"Accept=*/*","enctype=multipart/form-data"}, method = {RequestMethod.POST, RequestMethod.GET }, consumes="*/*")
public String uploadData_type( //
        @PathVariable("type") final String type, //
        @RequestParam("theDate") final String theDate, //        
        @RequestParam("data") final MultipartFile theFile //
        ) {
    String status_message = null;
    Tracking.message("Importing '%s' file: '%s'", type, theFile.getOriginalFilename());
     ..........
}

一旦脚本执行 curl 调用,就会从脚本中传递类型、日期和数据。

脚本代码:

#!/bin/bash

SILENT=--silent

THIS_SCRIPT=${0##*/}

FILETYPE=$1
theFile=$2
theFileName=${theFile##*/}
theDate=${theFileName##*.}

URL="http://localhost:8080/gds-report/uploadData/$FILETYPE"

msg=$(curl $SILENT -X POST  -F "data=@$theFile" -F "theDate=$theDate" -F "filename=$theFileName"  -H "Content-Type: multipart/form-data"  "$URL")
echo -n $THIS_SCRIPT: 
if [ -z "$msg" ] ; then
   echo "Results - no message received"
   exit 1
else
   echo "Results: $msg"
fi

我尝试了在 stackoverflow 上找到的 2 个解决方案: 1)编辑并尝试在请求中传递 csrf 参数或更改禁用 csrf 的方法类型。但是,没有运气,它总是给我一个 405 错误。 2) 在我的 app-servlet.xml 文件中添加这个 bean 以允许上传大文件。

<beans:bean id="multipartResover"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <beans:property name="maxUploadSize" value="2000000">
        </beans:property>
    </beans:bean>

我尝试使用 curl for windows 进行调试,POSTMAN 都给出了相同的错误。我在任何文档中都找不到更多关于此的信息。有人可以帮忙吗,我认为这是我所缺少的非常小的东西。

POSTMAN ERROR OUTPUT 下面显示了一个 Allow: GET 标头,即使我使用 POST 方法请求并且响应标头只有 Allow:GET,我也不明白它为什么会出现。

Allow → GET
Cache-Control → no-cache, no-store, max-age=0, must-revalidate
Content-Language → en
Content-Length → 1090
Content-Type → text/html;charset=ISO-8859-1
Date → Thu, 25 Feb 2016 08:54:26 GMT
Expires → 0
Pragma → no-cache
Server → Apache-Coyote/1.1
X-Content-Type-Options → nosniff
X-Frame-Options → DENY
X-XSS-Protection → 1; mode=block

注意:控制器中的 GET 请求已成功完成。只是POST根本不起作用。请给我一些建议。 Spring 4. Java 8 Tomcat 8.

【问题讨论】:

    标签: java spring-mvc curl restful-url tomcat8


    【解决方案1】:

    这里可能有两件事:您的防火墙规则可能会阻止发布操作本身,在这种情况下,您可以编写一个新的例外(或尝试不打开防火墙进行检查)。另一种可能性是路由器本身可能会阻止请求,就好像流量被视为“类似服务器的行为”,这可能需要将端口转发异常写入路由器。

    【讨论】:

    • 试过这个.. 并确保没有这样的规则。没有这样的防火墙规则.. localhost tomcat 配置也没有任何东西,除了默认的..
    • hayageek.com/php-curl-post-get/#curl-post 这篇文章处理 curl 中的 html 请求,但可能是一个我们都没有注意到的 sn-p。
    • 这根本不是相关的答案。我不是在谈论 PHP,它完全不同..!!我们正在谈论弹簧安全和身份验证。使用 spring 登录和 mvc 执行 POST 请求
    【解决方案2】:
    • 我想我找到了解决方案。问题在于从命令行使用 curl 为 POST 请求设置 csrf 令牌,因为这不会通过 spring 登录页面,所以 spring 不知道如何将 csrf 令牌和参数附加到 header 参数。
    • 执行此操作的方法是仅针对这个 POST 请求关闭 csrf,因为它是来自命令行的经过验证的 curl 请求,而不是可能导致跨域域问题的 CORS 请求。
    • 因此,将此类 REST 请求的 &lt;http&gt;&lt;intercept-url&gt; 规则分开,并在 security.xml 文件中使用 &lt;csrf disabled:true&gt; 关闭 CSRF。

    【讨论】:

      猜你喜欢
      • 2020-02-16
      • 2014-09-03
      • 2016-10-16
      • 2019-11-06
      • 1970-01-01
      • 2022-12-27
      • 2022-09-23
      • 1970-01-01
      • 2016-12-10
      相关资源
      最近更新 更多