Spring Data REST filtering data based on the user

前端 未结 4 1948
無奈伤痛
無奈伤痛 2020-12-15 11:25

If I have a repository setup like the following, making use of Spring Data REST, I can access the data at /receipts and see all data. However, I want to only return data fo

4条回答
  •  半阙折子戏
    2020-12-15 12:11

    For example, given a Repositoryfor SomeEntity you could override findAll method with a custom @Query filtering by attribute ownerwith value of`#{principal.username}

    @RepositoryRestResource(path = "some-entities", collectionResourceRel = "some-entities", itemResourceRel = "some-entity")
    interface SomeEntityRepository extends PagingAndSortingRepository {
      @Override
      @RestResource(exported = true)
      @Query("select someEntity from SomeEntity someEntity where someEntity.owner = ?#{principal.username}")
      Iterable findAll();
    }
    

提交回复
热议问题