Spring Expression Language和Spring Security 3:在@PreAuthorize中访问bean引用

时间:2011-04-21 11:40:31

标签: java spring spring-security

我正在尝试访问@PreAuthorize注释中的bean引用,如下所示:

@PreAuthorize("@testBean.getTestValue()")
public String testSpEL() {
    ....
}

我有一个配置如下的测试bean:

@Component(value="testBean")
public class TestBean {
    public boolean getTestValue() {
        return true;
    }
}

但是当我尝试访问testSpEL()方法时,我遇到了以下异常:

Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1057E:(pos 1): No bean resolver registered in the context to resolve access to bean 'testBean'
    at org.springframework.expression.spel.ast.BeanReference.getValueInternal(BeanReference.java:45)
    at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:52)
    at org.springframework.expression.spel.ast.SpelNodeImpl.getTypedValue(SpelNodeImpl.java:102)
    at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:97)
    at org.springframework.security.access.expression.ExpressionUtils.evaluateAsBoolean(ExpressionUtils.java:11)

我已经彻底完成了我的研究,但我找不到我需要在配置中更改的内容才能使其工作。有什么指针吗?

谢谢!

亲切的问候,Jonck

P.S。我正在使用Spring 3.0.5。以下似乎表明此类功能应该有效:

https://jira.springsource.org/browse/SPR-7173

3 个答案:

答案 0 :(得分:6)

我在SpringSource上发布了一个类似的问题,事实证明Spring Security 3.0.5中尚未支持上述功能。幸运的是,版本3.1.0.RC1支持它,但使用非标准的SpEL语法:

@PreAuthorize("testBean.getTestValue()")
public String testSpEL() {
    ....
}

以下是SpringSource论坛中帖子的网址: SpringSource forum thread

希望这有助于某人!

答案 1 :(得分:2)

在Spring Security 3.1中(自3.1.RC2起)

@PreAuthorize("testBean.getTestValue()") 

不起作用 - 相反,你必须写

@PreAuthorize("@testBean.getTestValue()")

请参阅https://jira.springsource.org/browse/SEC-1723

答案 2 :(得分:0)

对于任何坚持使用Spring Security 3.0.x的人,我有一个简单的解决方法。在application-securityContext.xml(或其他)中添加此类:

https://gist.github.com/3340059

它将一个BeanFactoryResolver注入到Spring Security代码中,这是Spring Security 3.1.x修复版所具有的。对语法的支持已在3.0.x中。它允许您使用3.1.x,ala:

中的语法

@PreAuthorize("@controller.theProperty")

相关问题