How to add Custom Interceptor in Spring data rest (spring-data-rest-webmvc 2.3.0) -
i working on spring data rest services & facing issue in custom interceptors. earlier used spring-data-rest-webmvc 2.2.0 & added interceptor in following way.
public requestmappinghandlermapping repositoryexporterhandlermapping() { requestmappinghandlermapping mapping = super .repositoryexporterhandlermapping(); mapping.setinterceptors(new object[] { new myinterceptor() }); return mapping; }
it worked fine me. when upgraded spring-data-rest-webmvc 2.3.0 version, noticed handlermapping hidden behind delegatinghandlermapping. hence tried add interceptor in following way.
in 1 of config class have extended repositoryrestmvcconfiguration class & override 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 of repository operations (findall() operation on repository) start failing. if removed interceptors operations worked fine. (in interceptor authenticate user.) hence unable understand problem here. adding interceptor in wrong way? there other way add interceptor?
you should not use repositorymapping.setinterceptors()
- destoys internal interceptors spring placed there, , that's reason methods stopped working.
i suggest override jpahelper()
method , put interceptors jpahelper
object in repositoryrestmvcconfiguration
. spring should them global interceptor list.
but, again, if need authentication, why not use spring security filter?
edit: solution above works repositoryresthandlermapping
, not basepathawarehandlermapping
.
i suggest declare custom mappedinterceptor
bean somewhere:
@bean public mappedinterceptor mymappedinterceptor() { return new mappedinterceptor(new string[]{"/**"}, new myinterceptor()); }
from understanding of source code spring should automatically add interceptor request handlers.
Comments
Post a Comment