Spring Data Rest - 使用Spring Security 过滤关联资源

4
考虑两个实体EntityOtherEntity,它们作为@RepositoryRestResource暴露,通过@ManyToOne关系链接(一个Entity可能有几个OtherEntities)。
您想使用Spring Security过滤集合资源,因此覆盖它们的存储库的Iterable<Entity> findAll()函数。
@RepositoryRestResource
@Repository
public interface EntityRepository extends CrudRepository<Entity, Long> {
    @Override
    @PostFilter("hasPermission(filterObject, 'READ')")
    Iterable<Entity> findAll();
}

在调用GET /api/v1/entities或者GET /api/v1/otherEntities时,你会得到相应的权限过滤结果。

但是当调用它们的关联资源,GET /api/v1/entities/:id/otherEntities,检索到的OtherEntity元素列表没有被过滤。

哪个Repository函数应该被覆盖以使关联资源也被过滤?

或者还有其他方法可以实现这一点吗?

1个回答

2
很遗憾,据我所知,Spring Data或JPA目前没有直接支持此功能的机制。(参见: https://jira.spring.io/browse/DATACMNS-293)
仅仅覆盖存储库方法是绝对不够的,因为这只控制了将返回哪个实体层次结构的根节点,为了支持这一点,您必须过滤每个映射实体...
如果您在底层使用Hibernate,则理论上可以使用Hibernate Filters来实现。因此,在基本实体上,您必须向其映射的每个实体添加一个过滤器-但是,那样就无法与默认的Spring Data Repository很好地配合使用,因此您需要进行其他自定义,例如example
不幸的是,这并不像人们希望的那样简单 :)

我知道我在某个地方读到过这个,谢谢你提供的票据参考,这次我会收藏它的 :) 如果没有更好的答案,我会接受你的回答。 - Anthony Drogon

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接