Spring data rest - 有没有办法限制支持的操作? [英] Spring data rest - Is there a way to restrict the supported operations?

查看:24
本文介绍了Spring data rest - 有没有办法限制支持的操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Spring(SpringBoot) 应用程序中将数据库中的数据作为 Restful API 公开.Spring Data Rest 似乎完全适合此活动的目的.

I want to expose data from a database as Restful APIs in a Spring(SpringBoot) application. Spring Data Rest appears to be an exact fit for purpose for this activity.

这个数据库对于我的应用程序需要是只读的.默认提供所有 HTTP 方法.有没有我可以用来限制(实际上是防止)其他方法被暴露的配置?

This database is read-only for my application needs. The default provides all the HTTP methods. Is there a configuration that I can use to restrict (in fact prevent) the other methods from being exposed?

推荐答案

来自 隐藏存储库 CRUD 方法:

16.2.3.隐藏存储库 CRUD 方法

16.2.3. Hiding repository CRUD methods

如果您不想在您的CrudRepository,你可以使用@RestResource(exported = false)通过覆盖您要关闭的方法并放置覆盖版本上的注释.例如,为了防止 HTTP用户从调用 CrudRepository 的删除方法,覆盖所有并将注解添加到被覆盖的方法中.

If you don’t want to expose a save or delete method on your CrudRepository, you can use the @RestResource(exported = false) setting by overriding the method you want to turn off and placing the annotation on the overriden version. For example, to prevent HTTP users from invoking the delete methods of CrudRepository, override all of them and add the annotation to the overriden methods.

@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 的删除版本但保留导出采用实体实例的版本.为了暂时,您可以导出删除方法,也可以不导出.如果你想要关闭它们,然后请记住,您必须同时注释两者导出 = false 的版本.

It is important that you override both delete methods as the exporter currently uses a somewhat naive algorithm for determing which CRUD method to use in the interest of faster runtime performance. It’s not currently possible to turn off the version of delete which takes an ID but leave exported the version that takes an entity instance. For the time being, you can either export the delete methods or not. If you want turn them off, then just keep in mind you have to annotate both versions with exported = false.

这篇关于Spring data rest - 有没有办法限制支持的操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆