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

Posted

技术标签:

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

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

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

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

【问题讨论】:

【参考方案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": "/"

【讨论】:

以上是关于如何在 Spring Boot 应用程序的嵌入式 untertow 中禁用 HTTP TRACE的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Spring Boot 中关闭嵌入式 Tomcat?

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

如何在 spring-boot 中拦截嵌入式 Tomcat 上的“全局”404

如何在带有嵌入式 tomcat 的 Spring Boot 应用程序中运行 hawt.io

如何禁用嵌入式数据库 Spring-boot spring-data-jpa

如何在带有嵌入式 tomcat 的 Spring Boot App 中设置域名