2

GETクエリパラメータでこのユーザを指定せずにfindAll()メソッドを使用して、現在のユーザ関連エンティティを返すためにSpring Data RestとSpring Securityを使用できますか?

私の唯一の解決策は、パラメータとして、ユーザーを渡すことですが、多分それは別のオプションは、SpringセキュリティでのSpringデータレスト - 現在のユーザですべてを検索

public interface InvoiceRepository extends CrudRepository<Invoice, Long> { 
@RestResource 
@PreAuthorize("hasRole('ROLE_ADMIN') or user?.username == authentication.name") 
List<Invoice> findAllByUser(@Param("user") User user); 

答えて

5

あなたが@queryの注釈でSPEL式でも使用でき、セキュリティのプロパティと式を作ることSpEL EvaluationContext extensionを使用することができますSpringSecurityContextから彼を取得することです。これは、あなたは、現在のユーザーに関連するもののみ、ビジネス・オブジェクトを取得することができます:

interface SecureBusinessObjectRepository extends Repository<BusinessObject, Long> { 

    @Query("select o from BusinessObject o where o.owner.emailAddress like ?#{hasRole('ROLE_ADMIN') ? '%' : principal.emailAddress}") 
    List<BusinessObject> findBusinessObjectsForCurrentUser(); 
} 

詳細はhereです。

+0

ありがとうございます! – ShaggyBathDuck

+0

@ShaggyBathDuckあなたを助けたupvoted答えを受け入れることを忘れないでください... – Cepr0

関連する問題