我有以下我正在应用的接口(interface)方法@PreAuthoriz
电子:
@PreAuthorize("doSomething(#user.id)")
void something(User user, List<User> accessList);
在哪里
User
是 hibernate 实体 目的。它给了我一个错误:org.springframework.expression.spel.SpelEvaluationException: EL1007E:(pos 13): Field or property 'id' cannot be found on null at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:204)
没有办法
user
参数为null,好像我去掉注解,在实现这个接口(interface)方法的方法中检查user的值,有一个有效的User
对象存在那里。此外,就在调用此方法之前,我已确保正确构造了用户对象。我真的不明白为什么
user
SPEL 解析器将字段视为 null
最佳答案
您可以在 Object lookupVariable(String name) 方法中使用调试器检查 MethodSecurityEvaluationContext 中发生了什么:
@Override
public Object lookupVariable(String name) {
Object variable = super.lookupVariable(name);
if (variable != null) {
return variable;
}
if (!argumentsAdded) {
addArgumentsAsVariables();
argumentsAdded = true;
}
因此您可以看到 addArgumentsAsVariables() 方法中真正发生的事情,因为在 Spring 中非常清楚地实现了方法参数到 SPEL 变量的转换。
关于 Spring 安全 : Why can't we access Hibernate entitiy parameters in @PreAuthorize?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10767469/