Spring 数据休息 - 具有 Spring 安全性的过滤器关联资源

Spring Data Rest - Filter Association resources with Spring Security

考虑两个实体 EntityOtherEntity 公开为 @RepositoryRestResource,由 @ManyToOne 关系链接(一个 Entity 可能有多个 OtherEntities)

您想使用 Spring 安全性来过滤集合资源,因此您覆盖了他们存储库的 Iterable<Entity> findAll() 功能。

@RepositoryRestResource
@Repository
public interface EntityRepository extends CrudRepository<Entity, Long> {
    @Override
    @PostFilter("hasPermission(filterObject, 'READ')")
    Iterable<Entity> findAll();
}

调用 GET /api/v1/entitiesGET /api/v1/otherEntities 时,您会得到相应的权限过滤结果。

但是在调用他们的关联资源 GET /api/v1/entities/:id/otherEntities 时,检索到的 OtherEntity 元素列表未被过滤。

应该覆盖哪个存储库函数以便关联资源也被过滤?

或者有其他方法可以实现吗?

不幸的是,据我所知,目前没有直接在 Spring 数据或 JPA 中支持这一点的机制。 (参见:https://jira.spring.io/browse/DATACMNS-293

仅仅覆盖存储库方法肯定是不够的,因为它只控制返回实体层次结构的哪个根,为了支持这一点,你必须过滤每个映射的实体......

如果您在后台使用休眠,理论上可以使用 Hibernate Filters. So on your base Entity you'd have to add a filter to each of it's mapped Entities - but then, that won't play nicely with the default Spring Data Repository, so you have additional customization to do, for example

不幸的是,这并不像人们希望的那么简单:)