如何在Spring启动应用程序的嵌入式untertow中禁用HTTP TRACE

时间:2018-04-19 06:40:04

标签: spring-boot http-status-code-405 undertow http-trace

我的目标是为我的Spring启动应用程序禁用HTTP TRACE方法,该应用程序使用嵌入式转发器。

首选工作yaml更改,如果没有,代码更改也可以。理想情况下,最终结果应该是4xx HTTP响应代码,并且响应中没有cookie值。 [spring.mvc.dispatch-trace-request:false给出了200 OK,所以对我没用。]

对此我不以为然,不应该这么难!

1 个答案:

答案 0 :(得分:3)

要在Spring Boot中禁用默认的跟踪处理,您可以在DispatcherServlet上覆盖doTrace处理程序的行为。

将此组件添加到您的spring config应该可以解决问题。 (示例在Kotlin中,但您可以轻松转换为Java)

@Component("dispatcherServlet")
class CustomDispatcherServlet : DispatcherServlet() {

    /**
     * We want to disable the default trace behaviour of returning the response
     * so we override the trace handling and process it as normal.
     * If the application doesn't support TRACE then a HTTP 405 response will be generated.
     */
    override fun doTrace(request: HttpServletRequest, response: HttpServletResponse) {
        processRequest(request, response)
    }
}

这会为TRACE请求生成此响应

{
    "timestamp": "2019-01-22T10:03:46.906+0000",
    "status": 405,
    "error": "Method Not Allowed",
    "message": "TRACE method is not allowed",
    "path": "/"
}
相关问题