如何将资源限制为当前用户拥有的资源

Lar*_*ans 5 spring spring-data-rest

我有一个父对象,它属于一个用户(在弹簧安全意义上)。

我想使用 spring-data-rest 的所有优点,但不必为了按当前用户进行过滤而覆盖一吨、修改查询等。

是否有捷径可寻?

总结一下,我想要这样的东西:

@PreAuthorize("hasRole('USER')")
@RepositoryRestResource(collectionResourceRel = "tasks", path="tasks")
public interface TaskRepository extends PagingAndSortingRepository<Task, Long> {}
Run Code Online (Sandbox Code Playgroud)

...当我转到“/tasks”时,它只显示属于经过身份验证的用户的任务。如果不需要,我不想使用像“/users/foo/tasks”这样的网址。

这是可行的吗?

Joh*_*tta 1

I suggest @Override the individual methods in order to secure the entity.

@PreAuthorize("hasRole('USER')")
@RepositoryRestResource(collectionResourceRel = "tasks", path="tasks")
public interface TaskRepository extends PagingAndSortingRepository<Task, Long> {
    @PostAuthorize("returnObject.owner.username == principal.username")
    Task findOne(Long id);
}
Run Code Online (Sandbox Code Playgroud)

http://docs.spring.io/spring-security/site/docs/current/reference/htmlsingle/#access-control-using-preauthorize-and-postauthorize