Spring Interceptor 在 Spring Data REST URL 中不起作用 [英] Spring Interceptor not working in Spring Data REST URLs

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

问题描述

我正在使用 Spring Data Rest 和 JPA 开发一个项目,我正在尝试配置一个 HTTP 拦截器.根据参考文档,在 Spring Web MVC Docs - Handler Mapping Interceptor,我创建了一个扩展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 **********

但是,当url被spring data rest使用时,我的拦截器没有被调用.这适用于所有 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 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆