如何在 .antmatchers.access() 中使用常量

how use constants in .antmatchers.access()

我想在 .antmatchers.access():

中使用 AuthoritiesConstants.java 中的一些常量

.antMatchers("/first/**").access("hasAuthority('ROLE_ALL') and hasAuthority('ROLE_ME')")

它有效,但我想使用常量 ALL:

public static final String ALL = "ROLE_ALL";

我试过了:

.antMatchers("/first/**").access("hasAuthority('AuthoritiesConstants.ALL')");

但这不起作用。

请帮忙

这是行不通的,因为它实际上并没有引用常量。它假设 'AuthoritiesConstants.ALL' 只是一个 String 文字,并且是这样计算的。

.antMatchers("/first/**").access("hasAuthority('AuthoritiesConstants.ALL')");

您可以尝试使用类似的东西:

.antMatchers("/first/**").hasAuthority(AuthoritiesConstants.ALL);

或者如果您需要在访问表达式中引用常量,您可以使用以下语法:access("hasAuthority(T(com.example.AuthoritiesConstants).ALL) and ..."),其中 com.example. 是包含 AuthoritiesConstants class 的包.