Spring Interceptor在Spring Data REST URL中不起作用

编程入门 行业动态 更新时间:2024-10-27 12:39:49
本文介绍了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中不起作用

本文发布于:2023-11-22 17:23:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1618342.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中不   Interceptor   Spring   Data   URL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!