Spring Data Rest:返回用户资源 [英] Spring Data Rest: Return Resources of User

查看:330
本文介绍了Spring Data Rest:返回用户资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring Boot和Spring Data Rest创建一个简单的CRUD-App(购物清单)。我有一个资源: ShoppingItem 。是否有一种简单的方法只返回属于发送请求的用户的资源? (多用户支持)因此,用户只能获得自己的ShoppingItems而不是每个ShoppingItem。
或者我必须自己实施一个控制器,我这样做了吗?

I'm creating a simple CRUD-App (a shoppinglist) with Spring Boot and Spring Data Rest. I have a resource: ShoppingItem. Is there an easy way to only return the resources which belong to the user who send the request? (Multiple User support) So a User only get his own ShoppingItems and not every ShoppingItem. Or do i have to implement a Controller by myself, where i do this?

我发现基于用户的Spring Data REST过滤数据这种基于用户过滤资源的方法,但这赢得了'帮助我找到存储库端点。

I found Spring Data REST filtering data based on the user this approach for filtering resources based on the user, but this won't help me for the repository endpoint.

提前致谢

推荐答案

如果您使用的是Spring安全集成你可以使用ACL(可能很重)或简单 postFilter 如下:

If you are using Spring security integration you can use ACL (maybe to heavy) or simple postFilter like following:

public interface ShoppingItemRepository extends CrudRepository<ShoppingItem, Long> {
    @PostFilter("filterObject.user.getId() == principal.id")
    @Override
    Iterable<ShoppingItem> findAll();   
}

这篇关于Spring Data Rest:返回用户资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆