英文:
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<?> 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("/**");
}
}
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论