I'm using spring-data-rest with spring-security.
Suppose I have this model:
public class Item {
private @Id @GeneratedValue Long id;
private final String description;
private final String username;
}
Its repository:
public interface ItemRepository extends CrudRepository<Item, Long> {
@Override
@PostFilter("filterObject.username == principal.username")
Iterable<Item> findAll();
}
findAll() method worked, it returns only items which belong to username.
Question 1: How to override method save()
to restrict user saves item only if it belong to him?
@Override
<S extends Item> S save(S s);
Question 2: Currently I use many @PostFilter for each method like findAll
and findOne
... How to restrict whole repository items to user at once, by one annotation, at repo level?
I saw this answer, but also want to ask for more pretty solution.