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

时间:2016-12-07 18:28:33

标签: spring spring-security spring-data-rest

考虑两个实体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/entitiesGET /api/v1/otherEntities时,您会获得相应权限过滤的结果。

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

应该覆盖哪些存储库功能,以便关联资源也被过滤?

还是有另一种方法可以实现这个目标吗?

1 个答案:

答案 0 :(得分:1)

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

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

如果您正在使用hibernate,理论上可以使用Hibernate Filters。因此,在您的基本实体上,您必须为每个映射的实体添加一个过滤器 - 但是,这将不能很好地与默认的Spring Data Repository一起使用,因此您需要对example进行其他自定义操作。 。

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