简体   繁体   English

如何在 Java SpringBoot 中拦截/禁用 HTTP 跟踪方法

[英]How to intercept/disable HTTP Trace Method in Java SpringBoot

In order to disable insecure http methods, I have used a request filter为了禁用不安全的 http 方法,我使用了请求过滤器

@Component
public class MethodFilter extends OncePerRequestFilter {

    private final String[] allowedMethods = new String[]{"PUT", "POST", "GET", "OPTIONS"};

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
            throws ServletException, IOException {
        if (Arrays.stream(allowedMethods).noneMatch(x -> x.equals(request.getMethod()))) {
            response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
        }
        filterChain.doFilter(request, response);
    }
}

This works perfectly for all methods except "TRACE".这适用于除“TRACE”之外的所有方法。 For trace method, this filter is not invoked and I get a echo back of the all the headers in response body对于跟踪方法,不调用此过滤器,我得到响应正文中所有标头的回显

TRACE /error HTTP/1.1
my-header: test
accept: */*
host: localhost:8087
accept-encoding: gzip, deflate, br
connection: keep-alive

For all other methods which are not in the list I get result as expected对于不在列表中的所有其他方法,我得到了预期的结果

{
    "timestamp": "2021-11-03T11:49:48.545+0000",
    "status": 405,
    "error": "Method Not Allowed",
    "message": "DELETE method is not allowed",
    "path": "/test"
}

Referring to doc it seems, trace requests are sent to frameworkservlet and processed there itself.似乎参考doc ,跟踪请求被发送到 frameworkservlet 并在那里自行处理。 Have tried setting spring.mvc.dispatch-trace-request=true but now the responses are clubbed like this (filter still not invoked)已尝试设置spring.mvc.dispatch-trace-request=true但现在响应像这样(过滤器仍未调用)

{
    "timestamp": "2021-11-03T11:49:48.545+0000",
    "status": 405,
    "error": "Method Not Allowed",
    "message": "TRACE method is not allowed",
    "path": "/test"
}TRACE /error HTTP/1.1
my-header: test
accept: */*
host: localhost:8087
accept-encoding: gzip, deflate, br
connection: keep-alive

My question is how can I make TRACE response as same as the other requests ?我的问题是如何使 TRACE 响应与其他请求相同?

Note: The solution in this thread did not work for me.注意:此线程中的解决方案对我不起作用。

What if you try configuring Spring Security for it ... The approach with writing the filter manually, it feels a bit low level ...如果尝试为它配置 Spring Security 会怎样……手动编写过滤器的方法,感觉有点低级……

@Override
protected void configure(HttpSecurity http) throws Exception
{
     http
    .authorizeRequests()
       // You can actually disable other methods here as well
      .antMatchers(HttpMethod.TRACE,"/**").denyAll()
    .httpBasic();
}

If this does not work for you ... according to the docs it says如果这对你不起作用......根据它说的文档

Note that HttpServlets default TRACE processing will be applied
in any case if your controllers happen to not generate a response
of content type 'message/http' (as required for a TRACE response)

Why don't you try setting content type header to message/http when responding to a TRACE request in your filter ?在响应过滤器中的TRACE请求时,为什么不尝试将内容类型标头设置为message/http

Another option is disabling this in the dispatcher servlet另一个选项是在调度程序 servlet 中禁用它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM