如何防止从我的MongoRepository导出某些HTTP方法?

问题描述 投票:24回答:2

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

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

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

根据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请求。

spring spring-data spring-data-mongodb spring-data-rest
2个回答
38
投票

感谢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);

}

1
投票

这是迟到的回复,但如果您需要阻止实体的全局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, ...));
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.