简体   繁体   中英

springboot java interceptor doesn't invoke TRACE commad

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<?> 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;
        }
    }
}

the interceptor config:

@Configuration
public class InterceptorConfig implements WebMvcConfigurer {

    @Autowired
    private BlockingHttpInterceptor blockingHttpInterceptor;

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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