Spring Data Rest - 是否有一种方法可以限制支持的操作?

22

我希望在一个Spring(SpringBoot)应用程序中将数据库中的数据暴露为Restful API。Spring Data Rest似乎是这项工作的最佳选择。

对于我的应用需求,这个数据库是只读的,默认情况下提供所有HTTP方法。是否有可以使用的配置来限制(实际上是防止)其他方法被暴露?


4
最方便的方法是使用Spring Security来阻止除GET请求以外的所有请求。SDR确实在这个领域提供了功能:http://docs.spring.io/spring-data/rest/docs/current/reference/html/#customizing-sdr.hiding-repository-crud-methods,但是通过Spring Security在HTTP级别处理会更简单。 - Alan Hay
4个回答

38

从Spring文档的隐藏存储库CRUD方法部分:

16.2.3 隐藏存储库CRUD方法

如果您不想在CrudRepository上公开保存或删除方法,则可以使用@RestResource(exported = false)设置,通过覆盖要关闭的方法并将注释放置在覆盖版本上。例如,要防止HTTP用户调用CrudRepository的delete方法,请覆盖所有这些方法并将注释添加到覆盖的方法中。

@RepositoryRestResource(path = "people", rel = "people")
interface PersonRepository extends CrudRepository<Person, Long> {

  @Override
  @RestResource(exported = false)
  void delete(Long id);

  @Override
  @RestResource(exported = false)
  void delete(Person entity);
}

重要的是你需要覆盖两个删除方法,因为出口器目前使用了一种相对简单的算法来确定使用哪个CRUD方法以便提高运行时性能。目前无法关闭仅接受ID的删除版本并保留只接受实体实例的版本。目前,您可以选择是否导出删除方法。如果您想关闭它们,则请记住您必须将两个版本的"exported"属性都注释掉。


1
如果您正在阅读此内容,请考虑为此工单点赞。SDR 可能存在危险性。 https://jira.spring.io/browse/DATAREST-1034 - Snekse
这在Spring Boot 2.0.2.RELEASE版本中仍然适用吗? - pitchblack408
1
我正在使用JpaRepository。当我尝试覆盖 void delete(Long id) 时,我收到错误消息:“方法未覆盖其超类的方法”。在这种情况下,我该怎么做? - pitchblack408
对我不起作用...启动,数据重置1.5.4发布版。 - Beezer
请参考@Snekse的答案,并使用RepositoryRestConfiguration.disableDefaultExposure()及其文档来仅导出明确标记的repos/methods。 - Lubo
显示剩余2条评论

12
截至2018年初,现在有仅显式声明要暴露的存储库方法的能力DATAREST-1176
请参阅RepositoryRestConfiguration 类型级别的Export false不允许在方法级别上重写为export true票(DATAREST-1034)已经被打开,但被关闭为DATAREST-1176的重复项。Oliver Gierke说:

我会把这个问题解决为目前DATAREST-1176的版本,但如果你需要其他任何东西,请随时重新打开。

它们不是完全相同的副本,1034中描述的功能可能更加用户友好,但现在至少有一些选项。

请问您能否解释一下这是如何实现的?我在任何地方都找不到相关信息。 - Hamid Mohayeji
@HamidMohayeji 这可能并不完全回答你的问题,但 ExposureConfiguration 可能是配置这个的最正确的方法。自定义默认曝光 https://docs.spring.io/spring-data/rest/docs/current/reference/html/#customizing-sdr.http-methods.default-exposureExposureConfiguration https://docs.spring.io/spring-data/rest/docs/current/api/org/springframework/data/rest/core/mapping/ExposureConfiguration.html - Snekse
@HamidMohayeji 如果上面的评论失败了,那么掌握RepositoryRestConfiguration并设置repositoryDetectionStrategy可能是一个选项。您可以搜索该类以查找对expose的使用,同时还有几个更改默认曝光的选项。可能有Spring Boot属性来调整这些事情。 - Snekse
如果您想使用JpaRepository,它仍然可以正常工作吗? - arxakoulini
@nikiforos 有一种方法可以找出来。 - Snekse

3

自从Spring Data REST 3.1版本以来,我们可以根据HTTP方法配置暴露。我使用以下代码片段禁用了PUT、PATCH、POST和DELETE方法的项目和集合的暴露:

@Component
public class SpringDataRestCustomization implements RepositoryRestConfigurer {

    @Override
    public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config, CorsRegistry cors) {
        ExposureConfiguration exposureConfiguration = config.getExposureConfiguration();
        exposureConfiguration.withItemExposure((metadata, httpMethods) -> httpMethods.disable(HttpMethod.PUT)
                        .disable(HttpMethod.PATCH).disable(HttpMethod.POST).disable(HttpMethod.DELETE))
                .withCollectionExposure((metadata, httpMethods) -> httpMethods.disable(HttpMethod.PUT)
                        .disable(HttpMethod.PATCH).disable(HttpMethod.POST).disable(HttpMethod.DELETE));
    }
}

1

Spring Boot默认将所有方法暴露给REST。您可以将其设置为false。

config.setExposeRepositoryMethodsByDefault(false);

如需更多信息,请参考org.springframework.data.rest.core.config.RepositoryRestConfiguration

以下是执行此操作的示例代码片段:

@Configuration
public class ApplicationRepositoryConfig implements RepositoryRestConfigurer {

    @Override
    public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config, CorsRegistry cors) {
        ..........
        config.setExposeRepositoryMethodsByDefault(false);
    }
}

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