本文介绍了Spring Interceptor在Spring Data REST URL中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring Data Rest和JPA进行项目,并且正在尝试配置HTTP拦截器.根据参考文档, 在 Spring Web MVC文档-处理程序映射拦截器,我创建了一个扩展HandlerInterceptorAdapter的组件,如下所示:

I am working on a project with Spring Data Rest and JPA and I am trying to configure an HTTP interceptor. As per the reference docs, available in Spring Web MVC Docs - Handler Mapping Interceptor, I created a component that extends HandlerInterceptorAdapter as follows:

@Component
public class DBEditorTenantInterceptor extends HandlerInterceptorAdapter {

    Logger logger = LoggerFactory.getLogger(DBEditorTenantInterceptor.class);

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
        throws Exception {
         logger.debug("********** INTERCEPTION SUCCESSFUL **********");
         return true;
    }
}

然后,通过扩展WebMvcConfig来注册拦截器(如 Spring Web MVC文档-配置拦截器

And then, registered the interceptor by extending WebMvcConfig (as explained in Spring Web MVC Docs - Config Interceptors

@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {

    @Autowired
    DBEditorTenantInterceptor dbEditorTenantInterceptor;

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

}

当我向Spring Data REST未使用的任何URL(例如/helloworld)发出HTTP请求时,拦截器将按预期工作,如我所见,记录器输出

When I issue HTTP requests to any URL that is not used by Spring Data REST such as /helloworld the Interceptor works as expected, as I see the logger output

017-10-26 13:16:24.689 DEBUG 17012 --- [p-nio-80-exec-4] c.c.v.d.DBEditorTenantInterceptor        : ********** INTERCEPTION SUCCESSFUL **********

但是,当Spring数据仓库使用URL时,不会调用我的拦截器.这适用于所有网址,例如/api/{模型中的现有实体}

However, when the URL is used by spring data rest, my interceptor is not called. This applies to all URLs like /api/{existing entity in model}

为什么没有为Spring Data Rest URL调用我的拦截器?我该怎么做才能使拦截器对所有请求起作用?

Why is my interceptor not called for Spring Data Rest URLs ? What can I do to make my interceptor work for all requests ?

非常感谢.

推荐答案

通过声明类型为MappedInterceptor的bean并将其注入到我的拦截器中-扩展了HandlerInterceptorAdapter,我的拦截器由Spring Data Rest拾取,现在可用于任何URL在应用程序上.

By declaring a bean of type MappedInterceptor and injecting it with my interceptor - which extends HandlerInterceptorAdapter, my interceptor was picked up by Spring Data Rest and now works for any URL on the application.

这转化为以下实现(代替了我原来的问题):

This translated to the following implementation (replacing the one in my original question):

@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {

    @Autowired
    DBEditorTenantInterceptor dbEditorTenantInterceptor;

    @Bean
    public MappedInterceptor dbEditorTenantInterceptor() {
        return new MappedInterceptor(new String[]{"/**"}, dbEditorTenantInterceptor);
    }

}

不幸的是,我在Spring文档中找不到对此的任何引用.

Unfortunately, I could not find any references to this on the Spring documentation.

这篇关于Spring Interceptor在Spring Data REST URL中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 17:45