我的目标是为我的 Spring Boot 应用程序禁用 HTTP TRACE 方法,该应用程序使用嵌入式 undertow。
最好进行有效的 yaml 更改,如果没有,更改代码也可以。理想情况下,最终结果应该是 4xx HTTP 响应代码,并且响应中没有 cookie 值。 [spring.mvc.dispatch-trace-request: false 给出 200 OK,所以这对我没有好处。]
打破我的头,不应该这么难!
要在 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": "/"
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)