【问题标题】:How can I disable HTTP TRACE in embedded untertow of a Spring boot application如何在 Spring Boot 应用程序的嵌入式 untertow 中禁用 HTTP TRACE
【发布时间】:2018-09-29 13:23:39
【问题描述】:

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

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

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

【问题讨论】:

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


    【解决方案1】:

    要在 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": "/"
    }
    

    【讨论】:

      猜你喜欢
      • 2018-07-02
      • 2018-01-02
      • 1970-01-01
      • 2019-06-11
      • 2018-05-29
      • 2017-07-11
      • 2020-07-18
      • 2017-11-08
      • 2017-10-21
      相关资源
      最近更新 更多