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

查看:201
本文介绍了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?

推荐答案

摘自Spring文档,位于

From the Spring docs on Hiding repository CRUD methods:

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的delete版本 但保留导出带有实体实例的版本的功能.为了 目前,您可以导出删除方法,也可以不导出.如果你 想要关闭它们,然后记住,您必须同时注释这两个 导出版本为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天全站免登陆