【发布时间】:2013-11-15 11:32:28
【问题描述】:
我在之前的帖子中问过这个问题:SpEL for spring security: Passing Values from XML to Java based SpEL configuration。但还没有解决。我想将 xml 配置或外部文件中的值注入 @PreAuthorize(...) 注释。像使用@Value注解注入一样不容易。
为了回忆这个问题,我提供以下信息。
-
我有以下 xml 配置文件 (example.xml) 具有属性并初始化其对应的值。
<beans> <bean id="userBean" class="x.y.User"> <property name="name" value="A"/> <property name="userId" value="33"/> <bean id="customerBean" class="x.y.Customer"> <property name="name" value="B"/> <property name="customerId" value="33"/> </bean> </beans> -
我有以下外部属性文件 (example.properties) 在 /WEB-INF 文件夹中。这个文件是一个 上述 XML 配置文件的替代方案。
user.id = 33 customer.id =33 -
我的 applicationContext.xml 文件中有属性策略持有者配置
<context:property-placeholder location="/WEB-INF/*.properties" ignore-unresolvable="true" /> <bean id="propertyConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" p:location="/WEB-INF/example.properties" p:ignoreUnresolvablePlaceholders="true" /> -
我有两个模型类:
User和Customerpublic class User { private int userId; public int getUserId() { return userId; } } public class Customer { private int customerId; public int getCustomerId(){ return customerId; } } -
我有另一个要限制的服务/控制器类 使用
@PreAuthorize注解的'edit'方法。The restriction: 方法允许(授权执行) 当且仅当'userId'和'customerId'被评估为相等!。要实现限制,我想考虑两种方式
通过将 xml 文件 (example.xml) 中的
'userId'和'customerId'值注入下面的表达式 1。我使用的表达方式 这是 Rob Winch 建议的(谢谢 Rob!)。然而,春天 无法评估表达式。-
通过将外部属性文件(example.properties)中的
'userId'和'customerId'值注入表达式2 以下。同样,spring 也无法计算这个表达式。@Service("..") or @Controller public class MyMainClass { //Expression 1 @PreAuthorize("@userBean.userId == @customerBean.customerId") public Boolean edit(User user, Customer custmer) { return true; } //Expression 2 ////I've tried other ways as well, but end up with similar exceptions @PreAuthorize("${user.id} == ${customer.id}") public Boolean edit(User user, Customer customer) { return true; } }
我的问题:
第一季度。我必须在 @PreAuthorize 注释中放入哪些正确的表达式才能将 xml 文件 (example.xml) 或属性文件 (example.properties) 中的值注入到 @PreAuthorize(...) 表达式中,然后才能轻松评估它?
第二季度。如果我犯了除表达式以外的错误,请指出我。
第三季度。对我来说,这就像一个 1,000,000.00 美元的问题,因为我厌倦了解决这个问题!!!。所以请尽可能多地帮助我!
【问题讨论】:
-
您的第二个表达式仅适用于
@Value注释,因为它们指的是占位符而不是表达式。你能发布/添加完整的堆栈跟踪吗?
标签: java spring spring-security spring-annotations spring-el