Spring 表达式无法解析类型

Tho*_*ger 2 java spring spring-security spring-el

我有一个注释的服务方法 @PostAuthorize

@PostAuthorize("hasPermission(returnObject, new ReadUserPermission())")
public Optional<User> find(String email) {
    // implementation
}
Run Code Online (Sandbox Code Playgroud)

一旦方法被调用,spring 就会抛出以下异常:

Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1003E:(pos 28): A problem occurred whilst attempting to construct an object of type 'ReadUserPermission' using arguments '()'
    at org.springframework.expression.spel.ast.ConstructorReference.findExecutorForConstructor(ConstructorReference.java:190) ~[spring-expression-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.expression.spel.ast.ConstructorReference.createNewInstance(ConstructorReference.java:151) ~[spring-expression-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.expression.spel.ast.ConstructorReference.getValueInternal(ConstructorReference.java:94) ~[spring-expression-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.expression.spel.ast.MethodReference.getArguments(MethodReference.java:147) ~[spring-expression-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.expression.spel.ast.MethodReference.getValueInternal(MethodReference.java:79) ~[spring-expression-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.expression.spel.ast.SpelNodeImpl.getTypedValue(SpelNodeImpl.java:113) ~[spring-expression-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:105) ~[spring-expression-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.security.access.expression.ExpressionUtils.evaluateAsBoolean(ExpressionUtils.java:11) ~[spring-security-core-3.1.2.RELEASE.jar:3.1.2.RELEASE]
    ... 86 common frames omitted
Caused by: org.springframework.expression.AccessException: Failed to resolve constructor
    at org.springframework.expression.spel.support.ReflectiveConstructorResolver.resolve(ReflectiveConstructorResolver.java:121) ~[spring-expression-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.expression.spel.ast.ConstructorReference.findExecutorForConstructor(ConstructorReference.java:181) ~[spring-expression-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    ... 93 common frames omitted
Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1005E:(pos 0): Type cannot be found 'ReadUserPermission'
    at org.springframework.expression.spel.support.StandardTypeLocator.findType(StandardTypeLocator.java:81) ~[spring-expression-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.expression.spel.support.ReflectiveConstructorResolver.resolve(ReflectiveConstructorResolver.java:61) ~[spring-expression-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    ... 94 common frames omitted
Run Code Online (Sandbox Code Playgroud)

ReadUserPermission 看起来像这样:

@Component
public class ReadUserPermission implements Permission<User> {

    @Override
    public boolean hasPermission(User user, User target) {
        // implementation
    }
}
Run Code Online (Sandbox Code Playgroud)

起初,类ReadUserPermission和接口Permission都没有用@Component. 我想也许 springs 需要注释来找到类型,但我是否注释接口或类或两者都没有关系。例外总是一样的。

Tho*_*ger 5

我忽略了文档的这一部分:

可以使用 new 运算符调用构造函数。的完全限定类名应该用于所有,但原语类型和字符串(其中整数,浮点等等,都可以使用)