39. @Query 中的安全表达式

现在,可以在查询中使用 Spring Security。例如:

@Repository
public interface MessageRepository extends PagingAndSortingRepository<Message,Long> {
	@Query("select m from Message m where m.to.id = ?#{ principal?.id }")
	Page<Message> findInbox(Pageable pageable);
}

这将检查Authentication.getPrincipal().getId()是否等于Message的接收者。请注意,此示例假设您已将主体自定义为具有 id 属性的 Object。通过公开SecurityEvaluationContextExtension bean,所有通用安全性表达在查询中都可用。