Spring security Spring安全性-自定义预授权注释?

Spring security Spring安全性-自定义预授权注释?,spring-security,Spring Security,我猜答案是否定的,但我想知道是否有一种方法可以创建如下自定义注释: @Documented @Inherited @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE, ElementType.METHOD}) @PreAuthorize("hasAuthority('" + PermissionRequired.value() + "')") //doesn't work public @interface Permissi

我猜答案是否定的,但我想知道是否有一种方法可以创建如下自定义注释:

@Documented
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
@PreAuthorize("hasAuthority('" + PermissionRequired.value() + "')") //doesn't work
public @interface PermissionRequired {
   String value();
}
所以我可以这样做:

@PermissionRequired("CREATE_USER")
public User createuser(User newUser){
//..
}
但是,似乎没有一种方法可以引用组合注释,就像我在上面所做的那样。这看起来不像@AliasFor可以解决的问题,因为我没有直接使用该字段,而是将它与另一个字符串连接起来

我在这里的目标是创建不需要任何SpEl的安全注释。

JSR-250来拯救

在应用程序类上启用它:

@EnableGlobalMethodSecurity(prePostEnabled = true, jsr250Enabled = true)
public class MyApplication {
//..
}
使用它们的注释:

@RolesAllowed({"ROLE_CREATE_USER"})
public User createuser(User newUser){
//..
}
不过,它似乎需要在您的权限上加上“角色”前缀