spring安全性:为什么我们不能在@PreAuthorize中访问Hibernate entitiy参数? [英] spring security : Why can't we access Hibernate entitiy parameters in @PreAuthorize?

查看:975
本文介绍了spring安全性:为什么我们不能在@PreAuthorize中访问Hibernate entitiy参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下接口方法,我正在应用 @PreAuthoriz e:

  @PreAuthorize(doSomething(#user.id))
void something(User user,List< User> accessList);

其中 User 是一个 Hibernate实体对象。它给了我一个错误:


org.springframework.expression.spel.SpelEvaluationException:
EL1007E :( pos 13):Field或属性 'ID' 不能在空在
org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:204)


$ b $发现b

没有办法让 user 参数为null,就好像我移除注解一样,然后在实现的方法中检查用户的值这个接口方法,在那里有一个有效的 User 对象。另外,在调用这个方法之前,我确定用户对象的构造是正确的。



我真的不知道为什么 user 字段被SPEL解析器视为null

解决方案

您可以使用调试器检查发生了什么在 MethodSecurityEvaluationContext 中,在 Object lookupVariable(String name)方法中:

  @Override 
public Object lookupVariable(String name){
Object variable = super.lookupVariable(name);

if(variable!= null){
返回变量;
}

if(!argumentsAdded){
addArgumentsAsVariables();
argumentsAdded = true;





$ b

所以你可以看到 addArgumentsAsVariables( )方法作为方法参数向SPEL变量的转换在Spring中非常清晰地实现。


I have the following interface method on which I am applying @PreAuthorize :

@PreAuthorize("doSomething(#user.id)")
void something(User user, List<User> accessList);

where User is a Hibernate entity object. It gives me an error :

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)

There is no way that the user parameter is null, as if I remove the annotation, and inspect the value of user in the method that implements this interface method, there is a valid User object present there. Additionally, just before calling this method, I have made sure that the user object is correctly constructed.

I really can't figure out why would the user field be considered null by the SPEL parser

解决方案

You can check with the debugger what's going on in MethodSecurityEvaluationContext, inside Object lookupVariable(String name) method:

    @Override
    public Object lookupVariable(String name) {
    Object variable = super.lookupVariable(name);

    if (variable != null) {
        return variable;
    }

    if (!argumentsAdded) {
        addArgumentsAsVariables();
        argumentsAdded = true;
    }

and so you can see what's really going on in the addArgumentsAsVariables() method as the convertion of method arguments to SPEL variables is implemented very clearly in Spring.

这篇关于spring安全性:为什么我们不能在@PreAuthorize中访问Hibernate entitiy参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆