使用枚举类型作为@RolesAllowed-Annotation 的值参数 [英] Use Enum type as a value parameter for @RolesAllowed-Annotation

查看:37
本文介绍了使用枚举类型作为@RolesAllowed-Annotation 的值参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 Java 企业应用程序,目前正在做 Java EE 安全方面的工作,以限制特定用户对特定功能的访问.我配置了应用服务器和所有东西,现在我使用 RolesAllowed-annotation 来保护方法:

I'm developing a Java enterprise application, currently doing Java EE security stuff to restrict access for particular functions to specific users. I configured the application server and everything, and now I'm using the RolesAllowed-annotation to secure the methods:

@Documented
@Retention (RUNTIME)
@Target({TYPE, METHOD})
public @interface RolesAllowed {
    String[] value();
}

当我使用这样的注释时,它工作正常:

When I use the annotation like this, it works fine:

@RolesAllowed("STUDENT")
public void update(User p) { ... }

但这不是我想要的,因为我必须在这里使用 String,重构变得困难,并且可能会出现拼写错误.因此,我想使用 Enum 值作为此注释的参数,而不是使用 String.枚举看起来像这样:

But this is not what I want, as I have to use a String here, refactoring becomes hard, and typos can happen. So instead of using a String, I would like to use an Enum value as a parameter for this annotation. The Enum looks like this:

public enum RoleType {
    STUDENT("STUDENT"),
    TEACHER("TEACHER"),
    DEANERY("DEANERY");

    private final String label;

    private RoleType(String label) {
        this.label = label;
    }

    public String toString() {
        return this.label;
    }
}

所以我尝试使用枚举作为这样的参数:

So I tried to use the Enum as a parameter like this:

@RolesAllowed(RoleType.DEANERY.name())
public void update(User p) { ... }

但随后我收到以下编译器错误,尽管 Enum.name 只返回一个字符串(它始终是常量,不是吗?).

But then I get the following compiler error, although Enum.name just returns a String (which is always constant, isn't it?).

注解属性 RolesAllowed.value 的值必须是常量表达式`

The value for annotation attribute RolesAllowed.value must be a constant expression`

我接下来尝试的是向我的 Enum 添加一个额外的最终字符串:

The next thing I tried was to add an additional final String to my Enum:

public enum RoleType {
    ...
    public static final String STUDENT_ROLE = STUDENT.toString();
    ...
}

但这也不能作为参数工作,导致同样的编译器错误:

But this also doesn't work as a parameter, resulting in the same compiler error:

// The value for annotation attribute RolesAllowed.value must be a constant expression
@RolesAllowed(RoleType.STUDENT_ROLE)

我怎样才能实现我想要的行为?我什至实现了我自己的拦截器来使用我自己的注解,这很漂亮,但是对于这样的一个小问题,代码行数太多了.

How can I achieve the behavior I want? I even implemented my own interceptor to use my own annotations, which is beautiful, but far too much lines of codes for a little problem like this.

免责声明

这个问题最初是一个 Scala 问题.我发现 Scala 不是问题的根源,所以我首先尝试在 Java 中执行此操作.

This question was originally a Scala question. I found out that Scala is not the source of the problem, so I first try to do this in Java.

推荐答案

我认为您使用枚举的方法行不通.我发现如果我将最后一个示例中的 STUDENT_ROLE 字段更改为常量字符串而不是表达式,编译器错误就会消失:

I don't think your approach of using enums is going to work. I found that the compiler error went away if I changed the STUDENT_ROLE field in your final example to a constant string, as opposed to an expression:

public enum RoleType { 
  ...
  public static final String STUDENT_ROLE = "STUDENT";
  ...
}

然而,这意味着枚举值不会在任何地方使用,因为您将在注释中使用字符串常量.

However, this then means that the enum values wouldn't be used anywhere, because you'd be using the string constants in annotations instead.

在我看来,如果您的 RoleType 类只包含一堆静态的最终 String 常量,您会更好.

It seems to me that you'd be better off if your RoleType class contained nothing more than a bunch of static final String constants.

要了解为什么您的代码无法编译,我查看了 Java 语言规范 (JLS).注释的 JLS 指出,对于注释带有 T 类型的参数和值 V,

To see why your code wasn't compiling, I had a look into the Java Language Specification (JLS). The JLS for annotations states that for an annotation with a parameter of type T and value V,

如果 T 是原始类型或 StringV 是一个常量表达式.

if T is a primitive type or String, V is a constant expression.

常量表达式包括其他的,

TypeName 形式的限定名称.标识符,引用常量变量

Qualified names of the form TypeName . Identifier that refer to constant variables

并且定义了 常量变量作为

原始类型或 String 类型的变量,它是最终的并使用编译时常量表达式进行初始化

a variable, of primitive type or type String, that is final and initialized with a compile-time constant expression

这篇关于使用枚举类型作为@RolesAllowed-Annotation 的值参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆