如何禁用Spring Data REST存储库的默认暴露?

36

我有一个使用Spring-data-rest的项目,有一个依赖项目仅使用Spring Data。这两个项目都有Spring Data repositories,并使用@EnableJpaRepositories来实现它们的repository接口,但是我只想在父项目中导出repositories。

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

如果我只能通过禁用@RepositoryRestResource来实现此操作,更糟糕的是,其他使用情况不同的项目将无法为这些repositories启用REST端点,则我的依赖项目将不得不仅包括Spring Data REST…

3个回答

63
回到这里,因为我正在寻找这个特定的设置。看起来现在已经实施了。在这种情况下,您希望设置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

参考资料


不错。但是我找不到应用程序属性的任何文档。 - Martin Schröder
2
确实,我已做相应更新。自我发布后,链接中的锚点似乎发生了变化。 - Brian
1
默认行为有点糟糕,我觉得 :-/ ... 这是一种“安全漏洞”设计吗? - Julien
我明白了,我终于移除了不必要的依赖项 org.springframework.boot:spring-boot-starter-data-rest - Julien
我真不敢相信,春天默认通过REST公开了所有的数据库存储库,这太疯狂了! - undefined

17

目前没有全局开关可供您查找。 我已为您提交此票以包含在下一个主要版本中。

不确定是否适用于您,但除非明确注释,否则当前不会公开包私有存储库接口。 如果您可以将所有这些库存储库设置为包保护,则可能比明确注释更有利。


3
将repos包设为私有。感谢您创建该问题单。 - gyoder

6
从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。唯一可见的是带有URI http://.../profilerel="profile"。有人知道如何隐藏这个“最后一个URI”吗?谢谢。 - Zaphod Beeblebrox
这个配置类应该定义在哪里?我可以将它定义在我的SpringBoot项目的单独配置包中吗?@53c - Kush Trivedi
嗨@KushTrivedi,是的,你可以在你的配置包中定义它。只需要为该类添加一个@Configuration注解,以便springboot在启动时将其拾取起来。此外,如果它不在与带有@SpringBootApplication的主方法首次执行的同一包(或子包)中,则可能需要手动指定路径。虽然我通常将事物保留在子包中,但对此并不100%确定。 - 53c
1
这是一种非常奇怪的行为。如果它保持启用状态,基本上会公开每个实体/存储库和每个查询,例如findSomethingByWhatever()将拥有自己的一组端点等。到底谁会想要这样的东西呢? - Stefan Falk

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接