不使用Spring ACL,实现基于实体"creator"的方法级安全检查

Without using Spring ACL, implementing a method level security check based on "creator" of an entity

正如许多其他人所指出的那样,在这种情况下,ACL 对于执行概念上如此简单的事情似乎有点过分了。

我正在 Spring 安全中寻找以下场景的简单实现:

我在控制器中有一个方法可以处理事件实体的删除请求。我需要 @PreAuthorize 表达式来检查以确保提交请求的用户的 ID 与请求所有者的 ID 匹配。也许通过 Event.getOwner().getId() 方法。

这看起来怎么样?谢谢

我在想类似的事情。事件控制器中的以下方法:

 @PreAuthorize("#event.getOwner().getId() == authentication.id")
 public void delete(@PathVariable("id") final Long id, @RequestBody final Event event) {
    repo.delete(id, event);
}

如果这是正确的用法,#event 从何而来?换句话说,'#' 表示什么?我问,因为此时我们在控制器中,所以,#event 指的是哪个事件?

你的想法不安全。要求在删除请求中将包括owner在内的事件信息作为请求体发送。有了这个,客户端可以欺骗所有者是当前用户而不是实际所有者。

假设 id 是一个事件的唯一 ID,我会建议像下面这样的最小解决方案:

@RequestMapping(value="/{id}", method=RequestMethod.DELETE)
@PreAuthorize("@repo.getOwnerOfEvent(#id) == authentication.name")
public void delete(@PathVariable("id") final Long id) {
    repo.deleteEvent(id);
}

上面的代码片段需要一个名为 "repo" 的 bean,它包含以下方法:

/**
 * Returns the owner username of the event with the specified id.
 */
String getOwnerOfEvent(long id);