我使用spring的PreAuthorize注解如下:
@PreAuthorize("hasRole('role')");
但是,我已经将“角色”定义为另一个类的静态字符串。如果我尝试使用这个值:
@PreAuthorize("hasRole(OtherClass.ROLE)");
我收到一个错误:
org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 14): Field or property 'OtherClass' cannot be found on object of type 'org.springframework.security.access.expression.method.MethodSecurityExpressionRoot'
有没有办法通过 PreAuthorize 注释访问这样的静态变量?
最佳答案
尝试以下使用 Spring 表达式语言来评估类型:
@PreAuthorize("hasRole(T(fully.qualified.OtherClass).ROLE)");
请务必指定完全限定的类名。
Documentation
https://stackoverflow.com/questions/17444856/