【问题标题】:Spring Data Rest - Filter Association resources with Spring SecuritySpring Data Rest - 使用 Spring Security 过滤关联资源
【发布时间】:2017-04-22 19:48:25
【问题描述】:

考虑两个实体EntityOtherEntity 暴露为@RepositoryRestResource,通过@ManyToOne 关系链接(Entity 可能有多个OtherEntities

您想使用 Spring Security 过滤集合资源,因此您覆盖了它们的 Repository 的 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 元素列表没有被过滤。

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

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

【问题讨论】:

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


    【解决方案1】:

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

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

    如果您在后台使用休眠模式,理论上可以使用Hibernate Filters。因此,在您的基础实体上,您必须为每个映射实体添加一个过滤器 - 但是,这与默认的 Spring Data Repository 不能很好地配合,因此您需要对example 进行额外的自定义。

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

    【讨论】:

    • 我知道我在某处读到过它,感谢您提供票证参考,这次将其加入书签 :) 如果没有更好的结果,将接受您的回答。
    猜你喜欢
    • 2018-09-04
    • 2014-10-08
    • 1970-01-01
    • 1970-01-01
    • 2014-04-25
    • 2015-08-30
    • 1970-01-01
    • 2018-10-08
    • 2018-09-07
    相关资源
    最近更新 更多