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

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

问题描述

我正在开发一个Java企业应用程序,目前正在做的Java EE安全性的东西来限制特定功能,对特定用户的访问。我配置了应用服务器和一切,现在我使用的RolesAllowed注解以固定方式:

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) { ... }

但是,这不是我想要的,我要在这里使用一个字符串,重构变硬,错别字可能发生。因此,而不是使用字符串,我想用一个枚举值作为这个注解的参数。枚举看起来是这样的:

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必须是常量前pression`

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

我想接下来的事情就是额外的最后字符串添加到我的枚举:

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)

我如何能实现我想要的行为?我甚至实现我自己的拦截用我自己的注解,这是美丽的,但$太多线C $ CS像这样的小问题。

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是不是问题的根源,所以我第一次尝试这样做在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 字段添加到字符串常量,而不是一个前pression:

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.

在我看来,如果你的角色类型类包含的无非是一堆静态最终字符串常量的越多,你会更好。

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.

要明白为什么你的code不编译,我看看到的 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 的原始类型或字符串 V 的是一个不断前pression。

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

一个不断前pression 包括,除其他事项外,

A constant expression includes, amongst other things,

限定名称的类型名的。的标识的引用变量不变

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

和一个常变量被定义为

一个变量,基本类型或键入字符串,即最终与编译时间常数前pression初始化

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

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

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