Spring MVC的CURL POST请求给出405方法'POST'不支持

维沙尔·莎(Vishal Shah)

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

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

我试图使用curl为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

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

维沙尔·莎(Vishal Shah)
  • 我想我找到了解决方案。问题是使用命令行的curl为POST请求设置csrf令牌,并且由于这不会通过spring登录页面进行,因此spring不知道如何将csrf令牌和参数附加到标头参数中。
  • 这样做的方法是仅针对这一个POST请求关闭csrf,因为它是来自命令行的经过验证的curl请求,而不是可能导致跨源域问题的CORS请求。
  • 因此,请针对此类REST请求分离出<http><intercept-url>规则,并<csrf disabled:true>在security.xml文件中使用来关闭CSRF

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Spring MVC-不支持请求方法“ POST”

来自分类Dev

HTTP状态405-具有Spring Security的Spring MVC不支持请求方法'POST'

来自分类Dev

Spring MVC HTTP状态405-请求方法'POST'不支持-骨干请求

来自分类Dev

上载文件时不支持Spring MVC错误405请求方法'POST'

来自分类Dev

Spring MVC上传文件-HTTP状态405-不支持请求方法“ POST”

来自分类Dev

使用Spring Security登录:请求方法'POST'不支持

来自分类Dev

Spring Security 4:不支持请求方法“ POST”

来自分类Dev

Spring Security不支持请求方法“ POST”

来自分类Dev

请求方法'POST'不支持Spring Boot

来自分类Dev

Spring Boot新手-请求方法'POST'不支持

来自分类Dev

MVC-请求的资源不支持http方法“ POST”

来自分类Dev

PUT不支持Spring MVC-405方法

来自分类Dev

Spring POST请求不支持的媒体类型415

来自分类Dev

Spring POST请求不支持的媒体类型415

来自分类Dev

POST上的Spring MVC不支持内容类型'application / json'

来自分类Dev

请求方法'GET'不支持Spring MVC

来自分类Dev

Spring MVC表单-请求方法'GET'不支持

来自分类Dev

“请求的资源不支持http方法'POST'-405响应

来自分类Dev

春季:请求方法'POST'不支持

来自分类Dev

HttpRequestMethodNotSupportedException:不支持请求方法“ POST”

来自分类Dev

不支持 Springboot 请求方法“POST”

来自分类Dev

Spring Boot - 不支持请求方法“POST”(不允许方法)

来自分类Dev

Web Api MVC 错误:(请求的资源不支持 http 方法“POST”。)

来自分类Dev

将 get 请求转发到 post 请求的 jattempting 返回不支持的错误 Spring

来自分类Dev

415 Spring应用程序中不支持POST请求的MediaType

来自分类Dev

Web API不支持POST方法

来自分类Dev

此路线不支持POST方法

来自分类Dev

Web API不支持POST方法

来自分类Dev

此路由不支持 post 方法

Related 相关文章

  1. 1

    Spring MVC-不支持请求方法“ POST”

  2. 2

    HTTP状态405-具有Spring Security的Spring MVC不支持请求方法'POST'

  3. 3

    Spring MVC HTTP状态405-请求方法'POST'不支持-骨干请求

  4. 4

    上载文件时不支持Spring MVC错误405请求方法'POST'

  5. 5

    Spring MVC上传文件-HTTP状态405-不支持请求方法“ POST”

  6. 6

    使用Spring Security登录:请求方法'POST'不支持

  7. 7

    Spring Security 4:不支持请求方法“ POST”

  8. 8

    Spring Security不支持请求方法“ POST”

  9. 9

    请求方法'POST'不支持Spring Boot

  10. 10

    Spring Boot新手-请求方法'POST'不支持

  11. 11

    MVC-请求的资源不支持http方法“ POST”

  12. 12

    PUT不支持Spring MVC-405方法

  13. 13

    Spring POST请求不支持的媒体类型415

  14. 14

    Spring POST请求不支持的媒体类型415

  15. 15

    POST上的Spring MVC不支持内容类型'application / json'

  16. 16

    请求方法'GET'不支持Spring MVC

  17. 17

    Spring MVC表单-请求方法'GET'不支持

  18. 18

    “请求的资源不支持http方法'POST'-405响应

  19. 19

    春季:请求方法'POST'不支持

  20. 20

    HttpRequestMethodNotSupportedException:不支持请求方法“ POST”

  21. 21

    不支持 Springboot 请求方法“POST”

  22. 22

    Spring Boot - 不支持请求方法“POST”(不允许方法)

  23. 23

    Web Api MVC 错误:(请求的资源不支持 http 方法“POST”。)

  24. 24

    将 get 请求转发到 post 请求的 jattempting 返回不支持的错误 Spring

  25. 25

    415 Spring应用程序中不支持POST请求的MediaType

  26. 26

    Web API不支持POST方法

  27. 27

    此路线不支持POST方法

  28. 28

    Web API不支持POST方法

  29. 29

    此路由不支持 post 方法

热门标签

归档