【问题标题】:@PreAuthorize and deleting by ID@PreAuthorize 并按 ID 删除
【发布时间】:2017-04-25 10:10:53
【问题描述】:

我希望为某些 Object o 提供围绕 CrudRepository 方法 delete(Integer id) 的安全性。我想确保即将删除的对象是由当前删除对象的人(或管理员)创建的。我一直在使用@PreAuthorize 作为授权方式。为了保护 delete(Object o) 方法,我所要做的就是

 @PreAuthorize("(#o.info.creator == authentication.principal.name) or (hasRole('ADMIN'))")
 @Override
 public void delete(@P("o")Object o);

但是,对于按 ID 删除,我无法找到一种方法来对照我们的主体名称检查生成的“创建者”。我最初的想法是这样的

@PreAuthorize("(**some Function Getting me the Object(#id)**.info.creator== authentication.principal.name)")
@Override
public void delete(@P("id")Integer id);

有没有办法在授权之前从 ID 获取到对象?或者我应该采取不同的方法

【问题讨论】:

标签: spring spring-security spring-data


【解决方案1】:

您可以尝试创建自己的注解@CanDeleteOnlyCreatorOrAdmin。然后创建方面(如果方法标记了此注释)将通过 id 获取要从数据库中删除的对象并检查当前经过身份验证的用户是管理员还是创建者。如果不是 - 抛出 403 。

你也可以使用@PostFilter,这里有详细信息: http://www.concretepage.com/spring/spring-security/prefilter-postfilter-in-spring-security

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-24
    • 2019-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多