我正在尝试访问@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)我已经彻底地完成了我的研究,但我找不到我需要在我的配置中进行哪些更改才能使其工作。有什么建议吗?
谢谢!
致以亲切的问候,容克
附注:我使用的是Spring 3.0.5。以下内容似乎表明此类型的功能应该可以工作:
发布于 2011-04-22 21:41:14
我在SpringSource上发布了一个类似的问题,结果发现Spring Security3.0.5还不支持上述功能。幸运的是,3.1.0.RC1版本确实支持它,尽管使用了非标准的SpEL语法:
@PreAuthorize("testBean.getTestValue()")
public String testSpEL() {
....
}这是SpringSource论坛上该帖子的url:SpringSource forum thread
希望这对某些人有帮助!
发布于 2012-07-24 16:55:10
在Spring Security 3.1中(从3.1.RC2开始)
@PreAuthorize("testBean.getTestValue()") 不起作用-相反,您必须编写
@PreAuthorize("@testBean.getTestValue()")发布于 2012-08-13 20:07:08
对于任何坚持使用Spring Security 3.0.x的人,我有一个稍微简单的变通办法。在您的应用程序中添加此类-securityContext.xml(或其他任何类):
https://gist.github.com/3340059
它将一个BeanFactoryResolver注入到Spring Security代码中,这是Spring Security 3.1.x修复所具有的全部内容。在3.0.x中已经支持该语法。它允许您使用3.1.x中的语法:
@PreAuthorize("@controller.theProperty")
https://stackoverflow.com/questions/5743565
复制相似问题