春季:@PreAuthorize是否优先于@Cacheable?

0x4*_*x42 2 spring spring-mvc spring-security spring-cache

我对Spring Security和Spring Caching有疑问。说我有一个方法,并用@PreAuthorize(“ condition”)和@Cacheable(...)注释了该方法,就像这样

@PreAuthorize("some authorization check")
@Cacheable(....)
public String calculate() {
  ....
}
Run Code Online (Sandbox Code Playgroud)

@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)?框架是否保证将对@PreAuthorize安全检查进行评估,即使calculate()函数的结果已被计算和缓存也是如此?请记住,可以由用户A调用此函数,然后由缓存中存储的值调用,然后另一个用户(用户B)执行需要再次调用此函数的操作?是否会评估@PreAuthorize条件?

从我在Spring文档中可以找到的内容,@ PreAuthorize和@Cacheable的建议顺序都定义为“ Ordered.LOWEST_PRECEDENCE”,我认为这意味着它们的评估顺序是不确定的?

谢谢!

Wil*_*ing 5

启用和批注的<security:global-method-security><cache:annotation-driven>标签都有一个属性,该属性确定其AOP建议的执行优先级。@PreAuthorize@Cacheableorder

如果要确保安全检查始终在“进行中”高速缓存检查之前进行,则应通过将order属性设置为较低的值来为其赋予较高的优先级。

<security:global-method-security order="1">
Run Code Online (Sandbox Code Playgroud)

  • 谢谢。但这不是在Spring缓存文档中应该提到的东西吗?我认为忽略方法上的其他注释非常严重。无论如何,我已经为高速缓存和全局方法安全性设置了order属性,我只是想知道是否@PreAuthorize检查是否以其他方式执行,以确保可以对其进行评估。 (4认同)