How to add Custom Interceptor in Spring data rest (spring-data-rest-webmvc 2.3.0)

江枫思渺然 提交于 2019-12-29 07:42:20

问题


I am working on the spring data rest services & facing some issue in the custom interceptors. Earlier I 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 perfectly fine for me. But when i upgraded to spring-data-rest-webmvc 2.3.0 version, I noticed that handlerMapping is hidden behind DelegatingHandlerMapping. Hence I tried to add interceptor in following way.

In one of my config class I have extended RepositoryRestMvcConfiguration class & override 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 this some of my repository operations (findAll() operation on repository) start failing. If I removed this interceptors those operations worked fine. (In this interceptor I am just authenticate the user.) Hence I am unable to understand problem here. Am I adding the interceptor in wrong way? Is there any other way to add the interceptor?


回答1:


You should not use repositoryMapping.setInterceptors() - it destoys the internal interceptors Spring placed there, and that's probably the reason some methods stopped working.

I suggest you override jpaHelper() method and put your interceptors into the JpaHelper object in RepositoryRestMvcConfiguration. Spring will should them to the global interceptor list.

But, again, if all you need is authentication, why not use a Spring Security filter?

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

I suggest you declare a custom MappedInterceptor bean somewhere:

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

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



来源:https://stackoverflow.com/questions/32916260/how-to-add-custom-interceptor-in-spring-data-rest-spring-data-rest-webmvc-2-3-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!