我不清楚 Spring Security 之间有什么区别:
@PreAuthorize("hasRole('ROLE_USER')")
public void create(Contact contact)
和
@Secured("ROLE_USER")
public void create(Contact contact)
我知道 PreAuthorize 可以与 spring el 一起使用,但在我的示例中,有真正的区别吗?
请您参考如下方法:
真正的区别是@PreAuthorize
可以与 Spring Expression Language (SpEL) 一起使用。您可以:
- 访问
SecurityExpressionRoot
的方法和属性. 访问方法参数(需要使用调试信息或自定义
ParameterNameDiscoverer
进行编译):@PreAuthorize("#contact.name == principal.name") public void doSomething(Contact contact)
- (高级功能)添加您自己的方法(覆盖
MethodSecurityExpressionHandler
并将其设置为<global-method-security><expression-handler ... /></...>
)。