我有一个关于 Spring Security 和 Spring Caching 的问题。假设我有一个方法,并且我已经用 @PreAuthorize("condition") 和 @Cacheable(...) 注释了该方法,就像这样
@PreAuthorize("some authorization check")
@Cacheable(....)
public String calculate() {
....
}
@PreAuthorize ( http://docs.spring.io/spring-security/site/docs/3.0.x/reference/ns-config.html ) 是否优先于 @Cacheable ( http://docs.spring.io/spring/docs/3.1.0.M1/spring-framework-reference/html/cache.html )?即使已经计算并缓存了calculate() 函数的结果,框架是否保证将评估@PreAuthorize 安全检查?请记住,这个函数可以被用户 A 调用,然后是存储在缓存中的值,然后另一个用户(用户 B)执行需要再次调用此函数的操作?是否会评估 @PreAuthorize 条件?
从我在 Spring Documenation 中可以找到的内容来看,@PreAuthorize 和 @Cacheable 的建议顺序都定义为“Ordered.LOWEST_PRECEDENCE”,我相信这意味着它们的评估顺序是未定义的?
谢谢!
最佳答案
两者<security:global-method-security>
和 <cache:annotation-driven>
启用 @PreAuthorize
的标签和 @Cacheable
注释,有一个 order
决定其 AOP 建议的执行优先级的属性。
如果你想确保安全检查总是在缓存检查“进入”之前发生,那么你应该通过设置 order
给它更高的优先级。归因于较低的值。
<security:global-method-security order="1">
关于Spring:@PreAuthorize 是否优先于 @Cacheable?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21462213/