Sha*_*uck 3 rest spring-security findall spring-data-rest spring-boot
是否可以使用Spring Data Rest和Spring Security返回当前用户相关实体,使用findAll()方法而不在GET查询参数中指定此用户?
我唯一的解决方案是将用户作为参数传递,但也许这是从SpringSecurityContext获取他的另一种选择
public interface InvoiceRepository extends CrudRepository<Invoice, Long> {
@RestResource
@PreAuthorize("hasRole('ROLE_ADMIN') or user?.username == authentication.name")
List<Invoice> findAllByUser(@Param("user") User user);
Run Code Online (Sandbox Code Playgroud)
您可以使用SpEL EvaluationContext扩展,使@Query注释中的SpEL表达式中的安全属性和表达式可用.这允许您仅获取与当前用户相关的业务对象:
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();
}
Run Code Online (Sandbox Code Playgroud)
更多细节在这里.
归档时间: |
|
查看次数: |
1579 次 |
最近记录: |