4

我的目标是为使用嵌入式 undertow 的 spring boot 应用程序禁用 HTTP TRACE 方法。

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

在这件事上打破我的头,不应该这么难!

4

1 回答 1

5

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

将此组件添加到您的 spring 配置中应该可以解决问题。(示例在 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": "/"
}
于 2019-01-22T10:51:39.183 回答