小编典典

在Spring注释中使用静态变量

java

我正在使用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批注访问此类静态变量?


阅读 270

收藏
2020-12-03

共1个答案

小编典典

尝试以下使用Spring Expression Language评估类型的方法:

@PreAuthorize("hasRole(T(fully.qualified.OtherClass).ROLE)");

确保指定完全限定的类名。

文献资料

2020-12-03