Spring Boot Java 拦截器未调用 TRACE 命令。

huangapple go评论69阅读模式
英文:

springboot java interceptor doesn't invoke TRACE commad

问题

我在spring-boot中定义了一个拦截器。
我重写了preHandle方法。
该拦截器对所有HTTP命令进行调用:GET/PUT/POST/PATCH/DELETE/HEAD/OPTIONS,
但不会对TRACE命令进行调用。

我漏掉了什么?

拦截器:

@Component
public class BlockingHttpInterceptor implements HandlerInterceptor {

    private final Class<?> thisClass = this.getClass();

    private String BASE_URL = "/subscribers";

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (HttpMethod.GET.matches(request.getMethod())
                || HttpMethod.POST.matches(request.getMethod())
                || (HttpMethod.DELETE.matches(request.getMethod()) && request.getRequestURI().startsWith(BASE_URL))
                || HttpMethod.PATCH.matches(request.getMethod())) {
            return true;
        } else {
            response.sendError(HttpStatus.METHOD_NOT_ALLOWED.value());
            return false;
        }
    }
}

拦截器配置:

@Configuration
public class InterceptorConfig implements WebMvcConfigurer {

    @Autowired
    private BlockingHttpInterceptor blockingHttpInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry){
        registry.addInterceptor(blockingHttpInterceptor).addPathPatterns("/**");
    }
}
英文:

I define an interceptor in spring-boot.
I override the preHandle method.
the interceptor is invoking for all HTTP commands : GET/PUT/POST/PATCH/DELETE/HEAD/OPTIONS
but it doesn't invoked for TRACE command.

what am I miss?

the interceptor:

@Component
public class BlockingHttpInterceptor implements HandlerInterceptor {

    private final Class&lt;?&gt; thisClass = this.getClass();

    private String BASE_URL = &quot;/subscribers&quot;;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (HttpMethod.GET.matches(request.getMethod())
                || HttpMethod.POST.matches(request.getMethod())
                || (HttpMethod.DELETE.matches(request.getMethod()) &amp;&amp; request.getRequestURI().startsWith(BASE_URL))
                || HttpMethod.PATCH.matches(request.getMethod())) {
            return true;
        } else {
            response.sendError(HttpStatus.METHOD_NOT_ALLOWED.value());
            return false;
        }
    }
}

the interceptor config:

@Configuration
public class InterceptorConfig implements WebMvcConfigurer {

    @Autowired
    private BlockingHttpInterceptor blockingHttpInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry){
        registry.addInterceptor(blockingHttpInterceptor).addPathPatterns(&quot;/**&quot;);
    }
}

答案1

得分: 0

如在JavaDoc中的DispatcherServlet中解释的,TRACE请求默认情况下不会被分派,因此它们永远不会到达您的控制器/拦截器。

幸运的是,您正在使用Spring Boot,它通过spring.mvc.dispatch-trace-request属性轻松配置此项,默认值为false。在您的application.properties中将其设置为true将启用TRACE请求的分派。

spring.mvc.dispatch-trace-request=true

将上述内容添加到您的属性中将启用它,并使事情按您的预期工作。

英文:

As explained in the JavaDoc for the DispatcherServlet the TRACE request are by default not dispatched, hence they will never reach your controllers/interceptor.

Luckily you are using Spring Boot which makes configuring this quite easy through the spring.mvc.dispatch-trace-request property, which is by default false. Setting this to true in your application.properties will enable dispatching for TRACE request.

spring.mvc.dispatch-trace-request=true

Adding the above to your properties will enable it and will make things work as you expect.

huangapple
  • 本文由 发表于 2023年1月9日 17:42:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75055425.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定