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

用户名

我正在研究spring数据剩余服务,并在自定义拦截器中遇到一些问题。之前我使用spring-data-rest-webmvc 2.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()操作)开始失败。如果我删除了此拦截器,则这些操作将正常进行。(在此拦截器中,我只是对用户进行身份验证。)因此,在这里我无法理解问题。我是否以错误的方式添加了拦截器?还有其他方法可以添加拦截器吗?

伊利亚·诺沃斯托尔采夫(Ilya Novoseltsev)

您不应该使用repositoryMapping.setInterceptors()-它会破坏Spring放置在其中的内部拦截器,这可能就是某些方法停止工作的原因。

建议您重写jpaHelper()method并将拦截器放入中的JpaHelper对象RepositoryRestMvcConfigurationSpring将它们添加到全局拦截器列表中。

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

编辑:以上解决方案仅适用于RepositoryRestHandlerMapping,不适用于BasePathAwareHandlerMapping

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

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将spring-data-rest-repository添加到spring-data-rest-webmvc

Spring-Data-Rest:当升级到spring-data-rest-webmvc 2.0.0时,Spring Data Book示例失败

Spring Data Rest:如何在HAL浏览器中公开自定义Rest控制器方法

spring-data-rest-webmvc:3.4.x 是否與 Spring Boot 2.3.x 兼容?

无法导入org.springframework.data.rest.webmvc.ResourceNotFoundException;

如何在自定义Spring Data Rest控制器中正确处理POST?

Spring Data Rest自定义参数解析器

在自定义控制器中接受Spring Data REST URI

如何在Spring Data Rest中将自定义标头添加到特定资源

Spring Data Rest / Spring Hateoas自定义控制器-PersistentEntityResourceAssembler

Spring Data Rest - 自定义端点名称

自定义Spring Data Rest @ManyToMany关系处理

Spring Data REST:自定义方法验证

Spring Data REST自定义查询集成

资源上的Spring Data Rest自定义链接

使用 Spring Data Rest 抛出自定义异常

Spring Data Rest将自定义端点添加到特定存储库

使用Spring Data REST的自定义控制器隐藏默认端点

Spring Data Rest,SpringFox和JpaRepository自定义查找器

使用Spring Data Rest @Projection作为自定义控制器中资源的表示

如何在Spring Data Rest中隐藏_embedded实体字段

如何在Spring Data REST中使用spring.data.rest.enable-enum-translation

如何在Spring Data(和Spring Data Rest)中通过Java Config配置审核?

Spring Data Rest绑定

JWT的Spring Data Rest

如何在Spring Data REST项目中使用DTO?

如何在Spring Data Rest GET方法中使用排序

如何在Spring Data Rest中使用内容协商?

如何自定义Spring Data REST以使用存储库资源的多段路径?