如何防止某些 HTTP 方法从我的 MongoRepository 导出?
How to prevent some HTTP methods from being exported from my MongoRepository?
我正在使用 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 请求。
感谢 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);
}
这是迟到的回复,但如果您需要阻止实体的全局 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, ...));
}
}
为什么不这样使用呢?
@Configuration
public class SpringDataRestConfiguration implements RepositoryRestConfigurer {
@Override
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration restConfig) {
restConfig.disableDefaultExposure();
}
}
我正在使用 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 请求。
感谢 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);
}
这是迟到的回复,但如果您需要阻止实体的全局 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, ...));
}
}
为什么不这样使用呢?
@Configuration
public class SpringDataRestConfiguration implements RepositoryRestConfigurer {
@Override
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration restConfig) {
restConfig.disableDefaultExposure();
}
}