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

Dip*_*ati 4 http-status-code-405 spring-boot undertow http-trace

我的目标是为我的 Spring Boot 应用程序禁用 HTTP TRACE 方法,该应用程序使用嵌入式 undertow。

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

打破我的头,不应该这么难!

Jac*_*lis 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)
    }
}
Run Code Online (Sandbox Code Playgroud)

这将为 TRACE 请求生成此响应

{
    "timestamp": "2019-01-22T10:03:46.906+0000",
    "status": 405,
    "error": "Method Not Allowed",
    "message": "TRACE method is not allowed",
    "path": "/"
}
Run Code Online (Sandbox Code Playgroud)