![](/img/trans.png)
[英]Using Autowired bean inside Preauthorize expression in Spring
[英]Spring Expression Language and Spring Security 3: accessing bean reference in @PreAuthorize
我正在尝试在@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
PS我正在使用Spring 3.0.5。 以下似乎表明此类功能应该起作用:
我在SpringSource上发布了一个类似的问题,事实证明Spring Security 3.0.5中尚未支持上述功能。 幸运的是,版本3.1.0.RC1支持它,但使用非标准的SpEL语法:
@PreAuthorize("testBean.getTestValue()")
public String testSpEL() {
....
}
这是SpringSource论坛的主题网址: SpringSource论坛帖子
希望这有助于某人!
在Spring Security 3.1中(自3.1.RC2起)
@PreAuthorize("testBean.getTestValue()")
不起作用 - 相反,你必须写
@PreAuthorize("@testBean.getTestValue()")
对于任何坚持使用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")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.