如何在 Spring data rest 中添加自定义拦截器 (spring-data-rest-webmvc 2.3.0) [英] How to add Custom Interceptor in Spring data rest (spring-data-rest-webmvc 2.3.0)

查看:43
本文介绍了如何在 Spring data rest 中添加自定义拦截器 (spring-data-rest-webmvc 2.3.0)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究 spring 数据休息服务 &在自定义拦截器中面临一些问题.早些时候我使用了 spring-data-rest-webmvc 2.2.0 &按以下方式添加拦截器.

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;
}

它对我来说非常好.但是当我升级到 spring-data-rest-webmvc 2.3.0 版本时,我注意到 handlerMapping 隐藏在 DelegatingHandlerMapping 后面.因此我尝试通过以下方式添加拦截器.

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.

在我的一个配置类中,我扩展了 RepositoryRestMvcConfiguration 类 &覆盖它的方法.

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);

    }
}

但是在添加这个之后,我的一些存储库操作(存储库上的 findAll() 操作)开始失败.如果我删除了这个拦截器,这些操作就可以正常工作.(在这个拦截器中,我只是对用户进行身份验证.)因此我无法理解这里的问题.我是否以错误的方式添加拦截器?有没有其他方法可以添加拦截器?

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?

推荐答案

你不应该使用 repositoryMapping.setInterceptors() - 它破坏了 Spring 放置在那里的内部拦截器,这可能是一些方法的原因停止工作.

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

我建议你重写 jpaHelper() 方法并将拦截器放入 RepositoryRestMvcConfiguration 中的 JpaHelper 对象中.Spring 会将它们添加到全局拦截器列表中.

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.

但是,如果您只需要身份验证,为什么不使用 Spring Security 过滤器呢?

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

上述解决方案仅适用于 RepositoryRestHandlerMapping,不适用于 BasePathAwareHandlerMapping.

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

我建议你在某处声明一个自定义的 MappedInterceptor bean:

I suggest you declare a custom MappedInterceptor bean somewhere:

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

根据我对源代码的理解,Spring 应该会自动将此拦截器添加到所有请求处理程序中.

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

这篇关于如何在 Spring data rest 中添加自定义拦截器 (spring-data-rest-webmvc 2.3.0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆