How to add custom interceptor to Spring datastore (spring -data-rest-webmvc 2.3.0)

I am working on spring data services and am encountering some problems in custom interceptors. I used to use spring -data-rest-webmvc 2.2.0 and added an interceptor as follows.

public RequestMappingHandlerMapping repositoryExporterHandlerMapping() { RequestMappingHandlerMapping mapping = super .repositoryExporterHandlerMapping(); mapping.setInterceptors(new Object[] { new MyInterceptor() }); return mapping; } 

It worked great for me. But when I upgraded to spring -data-rest-webmvc 2.3.0, I noticed that handlerMapping is hidden behind DelegatingHandlerMapping. So I tried to add an interceptor as follows.

In one of my configuration classes, I extended the RepositoryRestMvcConfiguration class and overriding its method.

 public class AppConfig extends RepositoryRestMvcConfiguration { @Autowired ApplicationContext applicationContext; @Override public DelegatingHandlerMapping restHandlerMapping() { RepositoryRestHandlerMapping repositoryMapping = new RepositoryRestHandlerMapping(super.resourceMappings(), super.config()); repositoryMapping.setInterceptors(new Object[] { new MyInterceptor()}); repositoryMapping.setJpaHelper(super.jpaHelper()); repositoryMapping.setApplicationContext(applicationContext); repositoryMapping.afterPropertiesSet(); BasePathAwareHandlerMapping basePathMapping = new BasePathAwareHandlerMapping(super.config()); basePathMapping.setApplicationContext(applicationContext); basePathMapping.afterPropertiesSet(); List<HandlerMapping> mappings = new ArrayList<HandlerMapping>(); mappings.add(basePathMapping); mappings.add(repositoryMapping); return new DelegatingHandlerMapping(mappings); } } 

But after adding some of my repository operations (the findAll () operation in the repository) starts to fail. If I deleted these interceptors, these operations worked fine. (In this interceptor, I just authenticate the user). So I can not understand the problem here. Am I adding an interceptor incorrectly? Is there any other way to add an interceptor?

+5
source share
2 answers

You should not use repositoryMapping.setInterceptors() - it destroys the internal Spring interceptors hosted there, and probably the reason some methods stopped working.

I suggest you override the jpaHelper() method and put your interceptors in a JpaHelper object in RepositoryRestMvcConfiguration . Spring will include them in the list of global interceptors.

But, again, if all you need is authentication, why not use the Spring Security Filter?

EDIT: The solution above only works for RepositoryRestHandlerMapping , not BasePathAwareHandlerMapping .

I suggest you specify a MappedInterceptor bean MappedInterceptor

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

From my understanding of Spring source code, you should automatically add this interceptor to all request handlers.

+10
source

Many thanks Ilya Novoseltsev , I followed your testimonies, and it worked perfectly. I just would like to mention that in my case I don't need to redefine the JpaHelper object in RepositoryRestMvcConfiguration . Instead, I implemented @Bean MappedInterceptor . For those who need to understand how to make a client interceptor and how it works, I think this is a clear and good example to run.

+1
source

Source: https://habr.com/ru/post/1232839/


All Articles