zoukankan      html  css  js  c++  java
  • java反射--注解的定义与运用以及权限拦截

    自定义注解类编写的一些规则:

    1. Annotation型定义为@interface, 所有的Annotation会自动继承Java.lang.Annotation这一接口,并且不能再去继承别的类或是接口.

    2. 参数成员只能用public或默认(default)这两个访问权修饰

    3. 参数成员只能用基本类型byte,short,char,int,long,float,double,boolean八种基本数据类型和String、Enum、Class、annotations等数据类型,以及这一些类型的数组.

    4. 要获取类方法和字段的注解信息,必须通过Java的反射技术来获取 Annotation对象,因为你除此之外没有别的获取注解对象的方法

    5. 注解也可以没有定义成员, 不过这样注解就没啥用了

    自定义注解类时, 可以指定目标 (类、方法、字段, 构造函数等) , 注解的生命周期(运行时,class文件或者源码中有效), 是否将注解包含在javadoc中及是否允许子类继承父类中的注解, 具体如下:

    1. @Target 表示该注解目标,可能的 ElemenetType 参数包括:

    ElemenetType.CONSTRUCTOR 构造器声明
    ElemenetType.FIELD 域声明(包括 enum 实例) 
    ElemenetType.LOCAL_VARIABLE 局部变量声明 
    ElemenetType.METHOD 方法声明 
    ElemenetType.PACKAGE 包声明 
    ElemenetType.PARAMETER 参数声明 
    ElemenetType.TYPE 类,接口(包括注解类型)或enum声明

    2. @Retention 表示该注解的生命周期,可选的 RetentionPolicy 参数包括

    RetentionPolicy.SOURCE 注解将被编译器丢弃 
    RetentionPolicy.CLASS 注解在class文件中可用,但会被VM丢弃 
    RetentionPolicy.RUNTIME VM将在运行期也保留注释,因此可以通过反射机制读取注解的信息

    3. @Documented 指示将此注解包含在 javadoc 中

    4.  @Inherited 指示允许子类继承父类中的注解

    类注解的定义:

    1. import java.lang.annotation.ElementType;  
    2. import java.lang.annotation.Retention;  
    3. import java.lang.annotation.RetentionPolicy;  
    4. import java.lang.annotation.Target;  
    5.   
    6. /** 
    7.  * 注解类 
    8.  * @author Owner 
    9.  */  
    10. @Retention(RetentionPolicy.RUNTIME)  
    11. @Target(ElementType.TYPE)  
    12. public @interface MyClassAnnotation {  
    13.     String uri();  
    14.     String desc();  
    15. }  

    构造方法注解定义:

    1. import java.lang.annotation.ElementType;  
    2. import java.lang.annotation.Retention;  
    3. import java.lang.annotation.RetentionPolicy;  
    4. import java.lang.annotation.Target;  
    5.   
    6. /** 
    7.  * 构造方法注解 
    8.  * @author Owner 
    9.  * 
    10.  */  
    11. @Retention(RetentionPolicy.RUNTIME)     
    12. @Target(ElementType.CONSTRUCTOR)   
    13. public @interface MyConstructorAnnotation {  
    14.   
    15.     String uri();  
    16.     String desc();  
    17. }  


    方法注解定义:

    1. import java.lang.annotation.ElementType;  
    2. import java.lang.annotation.Retention;  
    3. import java.lang.annotation.RetentionPolicy;  
    4. import java.lang.annotation.Target;  
    5.   
    6. /** 
    7.  * 我的方法注解 
    8.  * @author Owner 
    9.  * 
    10.  */  
    11. @Retention(RetentionPolicy.RUNTIME)     
    12. @Target(ElementType.METHOD)  
    13. public @interface MyMethodAnnotation {  
    14.   
    15.     String uri();  
    16.     String desc();  
    17. }  


    字段注解定义:

    1. import java.lang.annotation.ElementType;  
    2. import java.lang.annotation.Retention;  
    3. import java.lang.annotation.RetentionPolicy;  
    4. import java.lang.annotation.Target;  
    5.   
    6. /** 
    7.  * 字段注解定义 
    8.  * @author Owner 
    9.  * 
    10.  */  
    11. @Retention(RetentionPolicy.RUNTIME)     
    12. @Target(ElementType.FIELD)   
    13. public @interface MyFieldAnnotation {  
    14.   
    15.     String uri();  
    16.     String desc();  
    17. }  


    最后定义一个测试类

    1. import java.lang.reflect.Constructor;  
    2. import java.lang.reflect.Field;  
    3. import java.lang.reflect.Method;  
    4.   
    5. @MyClassAnnotation(desc="The class name", uri="com.annotation.MySample")  
    6. public class MyTest {  
    7.   
    8.     @MyFieldAnnotation(desc="The class field", uri="com.annotation.MySample#id")  
    9.     private int id;  
    10.       
    11.     @MyConstructorAnnotation(desc="The class constructor", uri="com.annotation.MySample#MySample")  
    12.     public MyTest(){}  
    13.   
    14.     public int getId() {  
    15.         return id;  
    16.     }  
    17.   
    18.     @MyMethodAnnotation(desc="The class method", uri="com.annotation.MySample#setId")  
    19.     public void setId(int id) {  
    20.         this.id = id;  
    21.     }  
    22.       
    23.       
    24.     public static void main(String[] args) throws Exception {  
    25.           
    26.         Class<MyTest> clazz = MyTest.class;  
    27.           
    28.         //得到类注解  
    29.         MyClassAnnotation myClassAnnotation = clazz.getAnnotation(MyClassAnnotation.class);  
    30.           
    31.         System.out.println(myClassAnnotation.desc()+" "+myClassAnnotation.uri());  
    32.           
    33.         //得到构造方法注解  
    34.         Constructor<MyTest> cons = clazz.getConstructor(new Class[]{});  
    35.           
    36.         MyConstructorAnnotation myConstructorAnnotation = cons.getAnnotation(MyConstructorAnnotation.class);  
    37.           
    38.         System.out.println(myConstructorAnnotation.desc()+" "+myConstructorAnnotation.uri());  
    39.           
    40.         //获取方法注解  
    41.         Method method = clazz.getMethod("setId", new Class[]{int.class});  
    42.           
    43.         MyMethodAnnotation myMethodAnnotation = method.getAnnotation(MyMethodAnnotation.class);  
    44.           
    45.         System.out.println(myMethodAnnotation.desc()+" "+myMethodAnnotation.uri());  
    46.         //获取字段注解  
    47.         Field field = clazz.getDeclaredField("id");  
    48.           
    49.         MyFieldAnnotation myFieldAnnotation = field.getAnnotation(MyFieldAnnotation.class);  
    50.           
    51.         System.out.println(myFieldAnnotation.desc()+" "+myFieldAnnotation.uri() );  
    52.     }  
    53.       
    54. }  


    输出:

    The class name com.annotation.MySample
    The class constructor com.annotation.MySample#MySample
    The class method com.annotation.MySample#setId
    The class field com.annotation.MySample#id


    好了,上面是基本学习,我们在实际的项目中用在什么地方呢?我想我们都做过关于细粒度权限拦截的问题,在Struts2中可以根据登录用户所具有的的权限进行任一个action方法的拦截,可以定义一个自定义方法注解,例如

    1. @Retention(RetentionPolicy.RUNTIME)//代表Permission注解保留在的阶段  
    2. @Target(ElementType.METHOD)//标注在方法上面  
    3. public @interface Permission {  
    4.   
    5.     /** 模块 */  
    6.     String module();  
    7.     /** 权限值 */  
    8.     String privilege();  
    9.       
    10. }  


    比如有一个部门action,Department.action,有一个方法public String departmentlistUI(){}


    可以这样定义方法

    1. @Permission(module="department",privilege="view")  
    2.     public String departmentlistUI(){  
    3. }  


    然后自定定义一个权限拦截器PrivilegeInterceptor.java并在struts.xml中注册,

    在实现interceptor接口后,实现方法public String intercept(ActionInvocation invocation) throws Exception {}


    在这里调用任一个action方法都会经过该拦截方法,通过invocation可以获取当前调用的action的名字,以及调用的action的哪个方法,


    通过这段代码可以获取action名字和方法名

    1. String  actionName=invocation.getProxy().getActionName();  
    2.         String  methodName=invocation.getProxy().getMethod();  
    3.           
    4.         System.out.println("拦截到:action的名字:"+actionName+"方法名:"+methodName);  


    然后通过反射技术,获取该方法上的自定义权限注解,获取当前登录的用户(从session中),遍历当前用户的所拥有的权限组,并且遍历任一个权限组下的所有的权限,看是否包括该方法上注解所需的权限。这样就可以完成细粒度的action方法权限拦截了。


    这只是个大体的思路

    下面看一下,拦截器的具体实现该功能的代码

      1. private boolean validate(ActionInvocation invocation) throws SecurityException, NoSuchMethodException {  
      2.           
      3.         String  methodName=invocation.getProxy().getMethod();  
      4.           
      5.         Method currentMethod = invocation.getAction().getClass().getMethod(methodName);  
      6.           
      7.         if(currentMethod != null && currentMethod.isAnnotationPresent(Permission.class)){  
      8.             //得到方法上的注解  
      9.             Permission permission = currentMethod.getAnnotation(Permission.class);  
      10.             //该方法上的所需要的权限  
      11.             SystemPrivilege methodPrivilege = new SystemPrivilege(new SystemPrivilegePK(permission.module(), permission.privilege()));  
      12.             //得到当前登录的用户  
      13.             Employee e = (Employee) ActionContext.getContext().getSession().get("loginUser");  
      14.             //遍历当前用户下的所有的权限组  
      15.             for(PrivilegeGroup group : e.getGroups()){  
      16.                 //如果该权限组下包含,要访问该方法所需要的权限,就放行  
      17.                 if(group.getPrivileges().contains(methodPrivilege)){  
      18.                     return true;  
      19.                 }  
      20.                   
      21.             }  
      22.             //说明遍历的该用户所有的权限组,没有发现该权限,说明没有该权限  
      23.             return false;  
      24.                
      25.         }  
      26.         //没有标注注解,表示谁都可以调用该方法  
      27.         return true;  
      28.     } 
  • 相关阅读:
    inet_ntoa解析
    日语常用计算机词汇
    Visual Studio 2012 : error LNK2026: module unsafe for SAFESEH image
    android异常总结四 :Unexpected text found in layout file: """
    android异常总结三 :R文件丢失
    android异常总结二 :This text field does not specify an inputType or a hint
    android异常总结一 :reslayoutOtherActivity.xml: Invalid file name: must contain only [a-z0-9_.]
    Win8下配置java环境
    CUDA实例练习(五):两数相加
    CUDA实例练习(四):矩阵转置
  • 原文地址:https://www.cnblogs.com/deepbreath/p/5533801.html
Copyright © 2011-2022 走看看