Spring Expression Language和Spring Security 3:在@PreAuthorize中访问bean引用
我正在尝试在@PreAuthorize注释中访问bean引用,如下所示:
@PreAuthorize("@testBean.getTestValue()") public String testSpEL() { .... }
我有一个配置如下的测试bean:
@Component(value="testBean") public class TestBean { public boolean getTestValue() { return true; } }
但是当我尝试访问testSpEL()方法时,我遇到了以下exception:
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
PS我正在使用Spring 3.0.5。 以下似乎表明此类function应该起作用:
https://jira.springsource.org/browse/SPR-7173
我在SpringSource上发布了一个类似的问题,事实certificateSpring Security 3.0.5中还没有支持上述function。 幸运的是,版本3.1.0.RC1支持它,但使用非标准的SpEL语法:
@PreAuthorize("testBean.getTestValue()") public String testSpEL() { .... }
这是SpringSource论坛的主题url: SpringSource论坛post
希望这有助于某人!
在Spring Security 3.1中(自3.1.RC2起)
@PreAuthorize("testBean.getTestValue()")
不起作用 – 相反,你必须写
@PreAuthorize("@testBean.getTestValue()")
请参阅https://jira.springsource.org/browse/SEC-1723
对于任何坚持使用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")