Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring正常关闭-不支持请求方法POST_Java_Spring_Spring Mvc_Spring Boot - Fatal编程技术网

Java Spring正常关闭-不支持请求方法POST

Java Spring正常关闭-不支持请求方法POST,java,spring,spring-mvc,spring-boot,Java,Spring,Spring Mvc,Spring Boot,我试图使用Spring端点优雅地关闭我的应用程序,但我遇到了一个错误: 2016-08-09 13:46:54.606 WARN 13315 --- [nio-8090-exec-6] .w.s.m.s.DefaultHandlerExceptionResolver : Handler execution resulted in exception: Request method 'POST' not supported 我正在使用指南,并已将我的应用程序.properties设置为具有端点

我试图使用Spring
端点
优雅地关闭我的应用程序,但我遇到了一个错误:

2016-08-09 13:46:54.606  WARN 13315 --- [nio-8090-exec-6] .w.s.m.s.DefaultHandlerExceptionResolver : Handler execution resulted in exception: Request method 'POST' not supported
我正在使用指南,并已将我的
应用程序.properties
设置为具有
端点。shutdown.enabled=true
端点。shutdown.sensitive=false
。我还在build.gradle中包含了
compile(“org.springframework.boot:springbootstarteractuator”)

当我发送CURL请求时:
CURL-xposthttps://localhost:8090/shutdown -k

我从服务器获得以下响应:

{"timestamp":1470747537792,"status":405,"error":"Method Not Allowed","exception":"org.springframework.web.HttpRequestMethodNotSupportedException","message":"Request method 'POST' not supported","path":"/shutdown"}

我做错什么了吗?有什么我可能遗漏的吗?我在整个应用程序中都启用了CSRF,因此对于我的应用程序,它不是禁用CSRF的选项。

您需要发送一个CSRF令牌作为标题或参数或其他内容。你的例子是:

curl -X POST https://localhost:8090/shutdown -k

不包括CSRF令牌,因此Spring当然会拒绝它。这就是CSRF滤波器的全部要点。您需要决定从该筛选器中排除
/shutdown
uri是否合适,或者是否需要存在令牌/nonce。

正如david所建议的,CSRF是必需的,因为Spring的所有
POST
请求都需要CSRF。因此,我能想到的绕过它的唯一方法是为
/shutdown
端点禁用CSRF

在我的
SecurityConfig
中,我设置:

http.csrf().ignoringAntMatchers("/shutdown");
这将仅对
/shutdown
url禁用csrf保护,同时对应用程序的其余部分保持活动状态


注意:此功能是在Spring 4中添加的。

我正在使用Springboot-1.5.3-RELEASE,请使用http方法POST(非GET)。它可以工作。

如果您使用Spring boot启动器执行器:

我也遇到了同样的问题,并将以下文本添加到src/main/resources文件夹中的application.properties文件中

management.endpoint.shutdown.enabled=true
management.endpoints.web.exposure.include=health,info,shutdown

重建应用程序,运行它并发送请求(例如:localhost:8080/exactor/shutdown)

如果不使用POST会发生什么?
{“timestamp”:1470748694304,“status”:405,“error”:“Method Not Allowed”,“exception”:“org.springframework.web.HttpRequestMethodNotSupportedException”,“message”:“请求方法”GET“Not supported”,“path”:“/shutdown”}
我知道需要CSRF,但如何向
/shutdown
URI发送CSRF令牌?有没有办法生成令牌并将其作为参数发送?我定义的不是请求映射,因为它是在Spring中预定义的,所以我不知道如何为请求提供头。