當柳上原的風吹向天際的時候...

          真正的快樂來源于創造

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            368 Posts :: 1 Stories :: 201 Comments :: 0 Trackbacks
          首先定義一個Annotation
          package com.heyang.anno;

          import java.lang.annotation.ElementType;
          import java.lang.annotation.Retention;
          import java.lang.annotation.RetentionPolicy;
          import java.lang.annotation.Target;

          @Target(ElementType.METHOD)
          @Retention(RetentionPolicy.RUNTIME)
          public @interface PrivilegeFor{
              
          int value() default 0;
          }

          其次在接口IDocService中使用這個Annotation設定執行方法所需要的權限值:
          package com.heyang.service;

          import com.heyang.anno.PrivilegeFor;
          import com.heyang.domain.Doc;
          import com.heyang.domain.User;

          /**
           * DocService需要實現的接口
           * 
          @author 何楊(heyang78@gmail.com)
           *
           * 
          @since 2008-12-30 下午05:16:17
           * 
          @version 1.00
           
          */

          public interface IDocService{
              
          /**
               * 用戶user添加一個文檔doc
               * 
          @param doc
               * 
          @param user
               
          */

              @PrivilegeFor(value
          =20)
              
          public void add(Doc doc,User user);
              
              
          /**
               * 用戶user刪除一個文檔doc
               * 
          @param doc
               * 
          @param user
               
          */

              @PrivilegeFor(
          60)
              
          public void delete(Doc doc,User user);
              
              
          /**
               * 用戶user更新一個文檔doc
               * 
          @param doc
               * 
          @param user
               
          */

              @PrivilegeFor(value
          =40)
              
          public void update(Doc doc,User user);
          }

          其三,在前置通知權限控制類中通過Annotation反射API得到方法需要的權限值,再根據用戶的權限值進行權限判定,不滿足則拋出異常,這樣權限不足的用戶將無法訪問DocService的相應方法。
          package com.heyang.service;

          import java.lang.reflect.Method;

          import org.springframework.aop.MethodBeforeAdvice;

          import com.heyang.anno.PrivilegeFor;
          import com.heyang.domain.User;
          import com.heyang.exception.PrivilegeNotEnoughException;

          /**
           * 實現權限子系統
           * 
          @author: 何楊(heyang78@gmail.com)
           * @date: 2009-1-2-下午04:19:13
           
          */

          public class PrivilegeService implements MethodBeforeAdvice{
              
          /**
               * 在IDocService的實際方法開始前進行前置處理--權限檢查
               
          */

              
          public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
                  
          // 取得方法名
                  String mothodName=arg0.getName();
                  
                  
          // 取得方法的標注
                  PrivilegeFor privilegeFor=arg0.getAnnotation(PrivilegeFor.class);
                  
                  
          if(privilegeFor!=null){
                      
          // 取得標注的值(即執行方法所需要的權限)
                      int value=privilegeFor.value();
                      
                      
          // 取得用戶權限
                      User user=(User)arg1[1];
                      
          int userPrivilegeValue=user.getPrivilegePoint();
                      
                      
          // 權限判斷
                      if(userPrivilegeValue<=value){
                          
          throw new PrivilegeNotEnoughException("用戶權限必須達到"+value+"才能執行"+mothodName+"操作");
                      }

                  }

              }

          }


          最后的結果和XML設置的方案是一致的。
          示例執行代碼如下:
              ApplicationContext ctx = new ClassPathXmlApplicationContext("context.xml");
                  
                  IDocService docService
          =(IDocService)ctx.getBean("docServiceProxy");
                  
                  Doc doc
          =new Doc("論美國次貸危機的產生及影響");
                  User user
          =new User("中科院經濟所研究員郭德綱",50);
                  
                  
          // 用戶向系統添加文章
                  try{
                      docService.add(doc, user);
                  }

                  
          catch(PrivilegeNotEnoughException ex){
                      System.out.println(ex.getMessage());
                  }

                  
                  
          // 用戶向系統更新文章
                  try{
                      doc.setName(
          "論美國次貸危機的產生及影響和我國應該采取的應對措施");
                      docService.update(doc, user);
                  }

                  
          catch(PrivilegeNotEnoughException ex){
                      System.out.println(ex.getMessage());
                  }

                  
                  
          // 用戶從系統撒刪除文章
                  try{
                      docService.delete(doc, user);
                  }

                  
          catch(PrivilegeNotEnoughException ex){
                      System.out.println(ex.getMessage());
                  }

              }

          執行效果
          將文件 名=論美國次貸危機的產生及影響交由dao處理(存入數據庫)
          將文件 名
          =論美國次貸危機的產生及影響和我國應該采取的應對措施交由dao處理(更新數據庫中對應的記錄)
          用戶權限必須達到60才能執行delete操作

          代碼下載
          http://www.aygfsteel.com/Files/heyang/PrivilegeAnnotation20090106155940.rar

          http://www.aygfsteel.com/Files/heyang/PrivilegeAnnotation220090108213912.rar

          需要自行載入的包為:
          commons-logging-1.0.4.jar,log4j-1.2.14.jar,spring.jar,mysql-connector-java-5.0.6-bin.jar
          posted on 2009-01-06 15:59 何楊 閱讀(482) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 中江县| 宁强县| 翼城县| 昌都县| 白银市| 拉萨市| 海城市| 梅州市| 平度市| 红原县| 庄浪县| 紫云| 翁牛特旗| 湟源县| 洛川县| 顺义区| 临漳县| 嘉善县| 东丰县| 灌南县| 赤城县| 噶尔县| 罗山县| 福鼎市| 乐亭县| 红桥区| 平泉县| 大英县| 遂川县| 吉木萨尔县| 建德市| 两当县| 张家港市| 格尔木市| 兖州市| 平塘县| 杭锦旗| 安仁县| 巴林左旗| 柞水县| 井陉县|