【问题标题】:How to prevent some HTTP methods from being exported from my MongoRepository?如何防止某些 HTTP 方法从我的 MongoRepository 中导出?
【发布时间】:2015-05-24 01:12:45
【问题描述】:

我正在使用 spring-data-rest 并且我有一个这样的 MongoRepository:

@RepositoryRestResource
interface MyEntityRepository extends MongoRepository<MyEntity, String> {
}

我想允许 GET 方法,但禁用 PUT、POST、PATCH 和 DELETE(只读网络服务)。

根据http://docs.spring.io/spring-data/rest/docs/2.2.2.RELEASE/reference/html/#repository-resources.collection-resource,我应该可以这样做:

@RepositoryRestResource
interface MyEntityRepository extends MongoRepository<MyEntity, String> {

    @Override
    @RestResource(exported = false)
    public MyEntity save(MyEntity s);

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

    @Override
    @RestResource(exported = false)
    public void delete(MyEntity t);
}

它似乎不起作用,因为我仍然可以执行 PUT、POST、PATCH 和 DELETE 请求。

【问题讨论】:

  • 应该可以,你能提供一个测试用例/测试项目来显示这个失败吗?
  • 今天再次测试后,确实有效。但是,我找不到如何限制 /myEntities 上的 GET 方法。将注解添加到 List&lt;MyEntity&gt; findAll(); 没有任何作用。
  • MongoRepository extends PagingAndSortingRepository 所以你需要重新声明和注解findAll(Pageable pageable)
  • 谢谢,我根据你的cmets做了一个回答。

标签: spring spring-data spring-data-mongodb spring-data-rest


【解决方案1】:

感谢 Oliver,以下是覆盖的方法:

@RepositoryRestResource(collectionResourceRel = "people", path = "people")
public interface PersonRepository extends MongoRepository<Person, String> {

    // Prevents GET /people/:id
    @Override
    @RestResource(exported = false)
    public Person findOne(String id);

    // Prevents GET /people
    @Override
    @RestResource(exported = false)
    public Page<Person> findAll(Pageable pageable);

    // Prevents POST /people and PATCH /people/:id
    @Override
    @RestResource(exported = false)
    public Person save(Person s);

    // Prevents DELETE /people/:id
    @Override
    @RestResource(exported = false)
    public void delete(Person t);

}

【讨论】:

  • 我可以禁用 put 吗?我希望通过 POST 创建所有新实体
  • 您可能会尝试注释 public Person insert(Person entity),但我怀疑它会起作用。
【解决方案2】:

这是迟到的回复,但是如果您需要阻止实体的全局http方法,请尝试。

@Configuration
public class DataRestConfig implements RepositoryRestConfigurer {
    @Override
    public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
         config.getExposureConfiguration()
                .forDomainType(Person.class)
                .withItemExposure(((metdata, httpMethods) -> httpMethods.disable(HttpMethod.PUT, HttpMethod.POST, ... )))
                .withCollectionExposure((metdata, httpMethods) -> httpMethods.disable(HttpMethod.PUT, HttpMethod.POST, ...));
    }
}

【讨论】:

    【解决方案3】:

    为什么不直接这样使用呢?

    @Configuration
    public class SpringDataRestConfiguration implements RepositoryRestConfigurer {
    
        @Override
        public void configureRepositoryRestConfiguration(RepositoryRestConfiguration restConfig) {
            restConfig.disableDefaultExposure();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-06-24
      • 2011-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多