【问题标题】:How to disable the default exposure of Spring Data REST repositories?如何禁用 Spring Data REST 存储库的默认公开?
【发布时间】:2015-04-04 12:37:39
【问题描述】:

我有一个使用 spring-data-rest 的项目,并且有一个只使用 Spring Data 的依赖项目。两个项目都有spring数据仓库,都使用@EnableJpaRepositories来实现它们的仓库接口,但是我只想导出父项目中的仓库。

这是我的问题:是否有某种方法可以将 Spring Data REST 配置为仅公开父项目中资源的 rest 端点,而无需使用 @RepositoryRestResource(exported = false) 显式注释依赖项项目中的每个存储库?

如果我只能通过 @RepositoryRestResource 禁用它来做到这一点,更糟糕的是,没有其他具有不同用例的项目能够为这些存储库启用 REST 端点,我的依赖项目将必须包含 Spring Data REST只为……

【问题讨论】:

    标签: java spring-data spring-data-rest


    【解决方案1】:

    目前没有针对您要查找的内容的全局开关。我已经为您提交了this ticket 以包含在下一个主要版本中。

    不确定它是否适合您,但包私有存储库接口当前不公开,除非明确注释。如果您可以将所有这些库存储库包保护起来,这可能比显式注释更有利。

    【讨论】:

    • 将 repos 包设为私有。感谢您创建该票证。
    【解决方案2】:

    在我寻找这个特定设置时循环回到这里。看起来现在已经实现了。在这种情况下,您需要设置 spring.data.rest.detection-strategy=annotated 以避免默认曝光。

    所有 application.properties 选项:

    # Exposes all public repository interfaces but considers @(Repository)RestResource\u2019s `exported flag.
    spring.data.rest.detection-strategy=default
    
    # Exposes all repositories independently of type visibility and annotations.
    spring.data.rest.detection-strategy=all
    
    # Only repositories annotated with @(Repository)RestResource are exposed, unless their exported flag is set to false.
    spring.data.rest.detection-strategy=annotated
    
    # Only public repositories annotated are exposed.
    spring.data.rest.detection-strategy=visibility
    

    参考文献

    【讨论】:

    • 不错。但我找不到应用程序属性的任何文档。
    • 确实,我进行了相应的更新。自从我发布后,URL 中的锚链接似乎发生了变化。
    • 我发现默认行为很糟糕:-/ ... 这种“安全漏洞”的设计是什么?
    • 我明白了,我终于删除了不需要的依赖org.springframework.boot:spring-boot-starter-data-rest
    【解决方案3】:

    从 3.4 版开始使用:

    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
    import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurer;
    import org.springframework.web.servlet.config.annotation.CorsRegistry;
    
    @Configuration
    public class SpringRestConfiguration implements RepositoryRestConfigurer {
        @Override
        public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config, CorsRegistry cors) {
            config.disableDefaultExposure();
        }
    }
    

    【讨论】:

    • 建议的配置实际上隐藏了大部分 URI。唯一可见的是 rel="profile" 和 uri http://.../profile。有谁知道如何隐藏“最后一个 uri 站立”?谢谢
    • 这个配置类应该在哪里定义?我可以在我的 springboot 项目的单独配置包中定义它吗? @53c
    • 嗨@KushTrivedi,是的,你可以在你的配置包中定义它。该类只需要一个 @Configuration 注释,以便 springboot 在启动时获取它。此外,如果它不在第一次执行带有@SpringBootApplication 的主要方法的同一包(或子包)中,那么您可能需要手动指定路径。不过,这不是 100% 确定的,因为我通常将东西放在子包中。
    猜你喜欢
    • 2017-08-23
    • 1970-01-01
    • 1970-01-01
    • 2016-04-30
    • 2018-05-25
    • 2019-08-04
    • 2017-04-30
    • 2015-03-13
    • 2018-02-23
    相关资源
    最近更新 更多