从Spring Security 4.0开始,您可以在Spring Data
JPA查询中访问安全上下文。
将
SecurityevaluationContextExtensionbean 添加到您的bean上下文中:
@Beanpublic SecurityevaluationContextExtension securityevaluationContextExtension() { return new SecurityevaluationContextExtension();}现在您应该可以
Principal在Spring Data查询中进行访问:
@Query("select count(m) from MyObject as m where m.user.id = ?#{ principal?.id }")@Overridelong count();@Modifying@Query("delete from MyObject as m where m.id = ?1 and m.user.id = ?#{ principal?.id }")@Overridevoid delete(Integer integer);@Modifying@Query("delete from MyObject as m where m.id = ?1 and m.user.id = ?#{ principal?.id }")@Overridevoid delete(MyObject entity);@Modifying@Query("delete from MyObject as m where m.user.id = ?#{ principal?.id }")@Overridevoid deleteAll();@Query("select 1 from MyObject as m where m.id = ?1 and m.user.id = ?#{ principal?.id }")@Overrideboolean exists(Integer integer);警告。查询可能有错误。我没有时间测试它。



