And*_*ewP 5 spring spring-mvc spring-data-jpa spring-data-rest spring-boot
我正在使用Spring Data Rest和JPA进行项目,并且正在尝试配置HTTP拦截器。根据Spring Web MVC Docs-Handler Mapping Interceptor中提供的参考文档,我创建了一个扩展HandlerInterceptorAdapter的组件,如下所示:
@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;
}
}
Run Code Online (Sandbox Code Playgroud)
然后,通过扩展WebMvcConfig来注册拦截器(如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("/**");
}
}
Run Code Online (Sandbox Code Playgroud)
当我向Spring Data REST未使用的任何URL(例如/ helloworld)发出HTTP请求时,拦截器按预期工作,如我所见,记录器输出
017-10-26 13:16:24.689 DEBUG 17012 --- [p-nio-80-exec-4] c.c.v.d.DBEditorTenantInterceptor : ********** INTERCEPTION SUCCESSFUL **********
Run Code Online (Sandbox Code Playgroud)
但是,当spring数据库使用URL时,不会调用我的拦截器。这适用于所有网址,例如/ api / {模型中的现有实体}
为什么没有为Spring Data Rest URL调用我的拦截器?如何使拦截器对所有请求起作用?
非常感谢。
通过声明类型为MappedInterceptor的bean并将其注入我的拦截器(扩展了HandlerInterceptorAdapter),Spring Data Rest拾取了我的拦截器,并且现在可以在应用程序上的任何URL上使用。
这转化为以下实现(替换了我原来的问题):
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
@Autowired
DBEditorTenantInterceptor dbEditorTenantInterceptor;
@Bean
public MappedInterceptor dbEditorTenantInterceptor() {
return new MappedInterceptor(new String[]{"/**"}, dbEditorTenantInterceptor);
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,我在Spring文档中找不到对此的任何引用。
| 归档时间: |
|
| 查看次数: |
2032 次 |
| 最近记录: |