【问题标题】:Spring: Does @PreAuthorize take precedence over @Cacheable?Spring:@PreAuthorize 是否优先于 @Cacheable?
【发布时间】:2014-02-23 02:14:54
【问题描述】:

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

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

谢谢!

【问题讨论】:

    标签: spring spring-mvc spring-security spring-cache


    【解决方案1】:

    启用@PreAuthorize@Cacheable 注释的<security:global-method-security><cache:annotation-driven> 标记都有一个order 属性来确定其AOP 建议的执行优先级。

    如果你想确保安全检查总是在缓存检查“进入”之前发生,那么你应该通过将order属性设置为较低的值来赋予它更高的优先级。

    <security:global-method-security order="1">
    

    【讨论】:

    • 谢谢。但这不是应该在关于缓存的 Spring 文档中提到的吗?我认为忽略方法上的其他注释是非常严重的。无论如何,我已经为缓存和全局方法安全设置了 order 属性,我只是想知道 @PreAuthorize 检查是否以其他方式执行以保证它们会被评估。
    猜你喜欢
    • 2015-04-17
    • 2014-10-23
    • 1970-01-01
    • 2018-11-20
    • 2017-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多