春天优雅关闭 - 请求方法发布不支持

时间:2016-08-09 12:52:26

标签: java spring spring-mvc spring-boot

我正在尝试使用Spring endpoints来优雅地关闭我的应用程序,但是我收到了一个错误:

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

我正在使用this指南,我已将application.properties设置为endpoints.shutdown.enabled=trueendpoints.shutdown.sensitive=false。我还在build.gradle中添加了compile("org.springframework.boot:spring-boot-starter-actuator")

当我发送CURL请求时:curl -X POST https://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,因此不能为我的应用程序禁用它。

4 个答案:

答案 0 :(得分:2)

您需要将CSRF令牌作为标头或参数等发送。你的例子:

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

不包含CSRF令牌,因此Spring当然会拒绝它。这确实是CSRF过滤器的重点。您需要决定是否适合从该过滤器中排除/shutdown uri,或者是否要求令牌/随机数存在。

答案 1 :(得分:1)

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

SecurityConfig我设置:

http.csrf().ignoringAntMatchers("/shutdown");

这会禁用/shutdown网址的csrf保护 ,同时保持其在应用程序的其余部分处于活动状态。

注意:此工具已在Spring 4中添加。

答案 2 :(得分:1)

如果您使用spring-boot-starter-actuator:

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

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

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

答案 3 :(得分:0)

我正在使用Springboot-1.5.3-RELEASE,使用http方法 POST (不是GET)。有效。