限制使用Java注释的方法的执行 [英] Restrict execution of a Method with Java Annotations

查看:180
本文介绍了限制使用Java注释的方法的执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你知道吗,如果有,检查谁在调用一个方法,限制他们是否被允许Java注解来执行它的可能性?

例如,如果你有一个客户端和服务器。有几个用户,其中有不同的角色,它们登录到客户端。然后(在相同的客户端)与不同的用户想要呼叫getMethod在服务器上

我可以限制,允许谁调用此methos Java注解?

我爱:

  @Role(角色=AllowedRole)
公众的返回类型实现getMethod(){
   ...
}


解决方案

好吧,我用缝/ DeltaSpike在JBoss服务器来实现这一目标。这是pretty简单。

基本上,你有你与你的注解注释的方法。例如,我是 @user

 公共类MyClass的{
    @用户
    公共对象实现getMethod(){
        //执行
    }
}

接下来,你需要一个定义如何检查注解类:

 公共类限制{    @Secures @user
    公共布尔isOk(身份识别){
        如果(identity.getUsername(彼得)){
            返回true;
        }
        返回false;
    }
}

这就是它! Ofcourse,你需要一些库,并在某些XML文件(如beans.xml文件)来定义这些拦截的东西,但它可以用一个小Googling轻松完成。

从这些链接启动:

Do you know, if there is the possibility to check who is calling a method and to restrict whether they are allowed to execute it with Java Annotations?

For example if you have a client and a server. There are several users, which have different roles and they login into the client. Then (the same client) with different users wants to call a getMethod on the server.

Can I restrict, who is allowed to call this methos with Java Annotations?

Like:

@Role(role="AllowedRole")
public ReturnType getMethod() {
   ...
}

解决方案

Well, I used to achieve this with Seam/DeltaSpike in JBoss Server. It's pretty straightforward.

Basically, you have a method which you annotate with your annotation. For example, mine is @User:

public class MyClass {
    @User
    public Object getMethod() {
        //implementation
    }
}

Next, you need a class where you define how you check your annotations:

public class Restrictions {

    @Secures @User
    public boolean isOk(Identity identity) {
        if (identity.getUsername("Peter")) {
            return true;
        }
        return false;
    }
}

That's it! Ofcourse, you need some libraries and to define these intercepting stuff in certain xml files (like beans.xml) but it can be easily done with a little googling.

Start from these links:

这篇关于限制使用Java注释的方法的执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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