根据issues:14480和文档,我希望在Spring Security 6.3.3中使用元注释。 但我仍然无法正确启用元注释。 我收到以下错误消息:
org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'roles' cannot be found on object of type 'org.springframework.security.access.expression.method.MethodSecurityExpressionRoot' - maybe not public or not valid?
以下是我复现的代码:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@SpringBootApplication
@RestController
@EnableMethodSecurity
public class SpringProjectApplication {
public static void main(String[] args) {
SpringApplication.run(SpringProjectApplication.class, args);
}
@GetMapping(value = "/get")
@HasAnyRole(roles = {"'USER'", "'ADMIN'"})
public String get() {
return "1";
}
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@PreAuthorize("hasAnyRole({roles})")
public @interface HasAnyRole {
String[] roles();
}
}
我使用的SpringBoot版本是3.3.4(对应Spring Security版本6.3.3)。
我是否遗漏了什么?