当前位置: 首页 > 知识库问答 >
问题:

如何在Spring data rest(spring-data-rest-webmvc 2.3.0)中添加自定义拦截器

宋腾
2023-03-14

我正在研究spring data rest服务&在自定义拦截器方面面临一些问题。前面我使用了spring-data-rest-webmvc2.2.0&以以下方式添加了拦截器。

public RequestMappingHandlerMapping repositoryExporterHandlerMapping() {
        RequestMappingHandlerMapping mapping = super
                .repositoryExporterHandlerMapping();

        mapping.setInterceptors(new Object[] { new MyInterceptor() });

        return mapping;
}

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

在我的一个配置类中,我扩展了RepositoryRestMvcConfiguration类&重写它的方法。

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()操作)开始失败。如果我拆除了拦截器,那些行动就会很顺利。(在这个拦截器中,我只是对用户进行身份验证。)因此我不能理解这里的问题。我添加拦截器的方式不对吗?有没有其他方法可以添加拦截器?

共有1个答案

孙昂然
2023-03-14

您不应该使用RepositoryMapping.setInterceptors()-它会删除Spring放置在那里的内部拦截器,这可能是某些方法停止工作的原因。

我建议您重写jpahelper()方法,并将拦截器放入RepositoryRestMvcConfiguration中的jpahelper对象。Spring会将他们列入全球拦截者名单。

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

编辑:上述解决方案仅适用于RepositorYresthAndlerMapping,不适用于BasePathawareHandlerMapping

我建议您在某个地方声明一个自定义的MappedInterceptorbean:

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

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

 类似资料:
  • 在RestTemplate中,我有一个自定义拦截器,它将记录一些请求-响应详细信息并保存到数据库。 我的自定义拦截器: springboot中的RestTemboard bean配置: 将拦截器添加到restTemboard bean: 如何将此拦截器添加到佯装客户端? 正在应用中。yml: InterceptorOne为假装客户端中的每个请求添加标头: 但是我不能添加日志服务拦截器,因为它由于错

  • 正如我们所知,在SpringDataRest中,存储库文件只被使用(而不是控制器),我们可以为它使用内置方法。 我不想添加自定义方法,并在那里添加我的请求处理逻辑。我想要一些配置或事件重写,在这里我可以处理HttpRequest处理程序,解析令牌并检查令牌中的一些数据,根据该令牌,我将决定要么处理该请求,要么放弃它,但出现一些错误。 谢了。

  • 这里是我的拦截器方法,我想在这里设置自定义响应,告诉UI发生了什么 并且在web.xml中 spring-servlet.xml 当会话超时时,它在返回false后不发送任何响应。连下面的都不行

  • 本文向大家介绍SpringBoot添加自定义拦截器的实现代码,包括了SpringBoot添加自定义拦截器的实现代码的使用技巧和注意事项,需要的朋友参考一下 在Controller层时,往往会需要校验或验证某些操作,而在每个Controller写重复代码,工作量比较大,这里在Springboot项目中 ,通过继承WebMvcConfigurerAdapter,添加拦截器。 1、WebMvcConfi

  • 问题内容: 我正在研究Spring Data JPA。考虑下面的示例,默认情况下我将使所有crud和finder功能正常工作,如果我想自定义finder,那么也可以在界面本身中轻松完成。 我想知道如何为上述AccountRepository的实现添加完整的自定义方法?由于它是一个接口,所以我不能在那里实现该方法。 问题答案: 你需要为自定义方法创建一个单独的接口: 并提供该接口的实现类: