<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    andyj2ee

    java tec sky

    統計

    留言簿(4)

    activemq

    aop

    design pattern

    other blog

    spring

    workflow

    多線程

    軟件架構師

    閱讀排行榜

    評論排行榜

    基于spring aop 權限管理系統原型<1>

    111-spring_aop.gif

    此權限管理系統把待訪問的業務層方法做為權限管理中的資源,通過spring aop 對接口方法進行攔截,來實現權限的管理,可以實現細粒度的權限控制。
    初步采用捕獲權限否決異常實現。代碼如下:
    資源接口:

    public interface ResourceBean {
        
    public void theMethod();
        
    public String getMethod1()throws PermissionDeniedException;
        
    public String getMethod2()throws PermissionDeniedException;
        
    public String getMethod3()throws PermissionDeniedException;
    }

    資源實現類
    public class ResourceBeanImpl implements ResourceBean {

        
    /* (non-Javadoc)
         * @see com.jhalo.jsecurity.aop.ResourceBean#theMethod()
         
    */

        
    public void theMethod(){
            System.
    out.println(this.getClass().getName()
                    
    + "." + new Exception().getStackTrace()[0].getMethodName()
                    
    + "()"
                    
    + " says HELLO!");
        }


        
    /* (non-Javadoc)
         * @see com.jhalo.jsecurity.aop.ResourceBean#getMethod1()
         
    */

        
    public String getMethod1() throws PermissionDeniedException{
            
    return "張三";
        }


        
    /* (non-Javadoc)
         * @see com.jhalo.jsecurity.aop.ResourceBean#getMethod2()
         
    */

        
    public String getMethod2() throws PermissionDeniedException{
            
    return "李四";
        }


        
    /* (non-Javadoc)
         * @see com.jhalo.jsecurity.aop.ResourceBean#getMethod3()
         
    */

        
    public String getMethod3() throws PermissionDeniedException{
            
    return "王五";
        }


    }

    服務層接口:
    public interface Service {
        
    public String getBeanInfo() throws PermissionDeniedException;
    }

    服務層接口實現類:
    public class ServiceBean implements Service {
        ResourceBean bean;
        
        
    /**
         * @param b The b to set.
         
    */

        
    public void setBean(ResourceBean bean) {
            
    this.bean = bean;
        }

        
    public String getBeanInfo(){
            String result
    ="";
            
    try{
                result
    += bean.getMethod1();
            }
    catch(PermissionDeniedException pde){
                result
    +="";
            }

            
    try{
                result
    += bean.getMethod2();
            }
    catch(PermissionDeniedException pde){
                result
    +="";
            }

            
    try{
                result
    += bean.getMethod3();
            }
    catch(PermissionDeniedException pde){
                result
    +="";
            }

            
    return result;
        }


    }

    用戶權限類:
    public class User {
        List privilages 
    = new java.util.ArrayList();
        String name;
        
    public User(){
            name
    ="tester";
            privilages.add(
    "com.jhalo.jsecurity.aop.ServiceBean.getBeanInfo");
            privilages.add(
    "com.jhalo.jsecurity.aop.ResourceBeanImpl.getMethod1");
    //        privilages.add("com.jhalo.jsecurity.aop.ResourceBeanImpl.getMethod2");
            privilages.add("com.jhalo.jsecurity.aop.ResourceBeanImpl.getMethod3");
        }

        
    public String getName(){
            
    return name;
        }

        
    public boolean isPermission(String pri){
            java.util.Iterator it 
    = privilages.iterator();
            String p 
    = "";
            boolean pass
    =false;
            
    while(it.hasNext()){
                
                p
    =(String)it.next();
                System.
    out.println(p);
                
    if(p.equals(pri)){
                    pass 
    = true;
                    
    break;
                }

            }

            
    return pass;
        }


    }


    權限驗證aspect
    public class PermissionCheckAdvice implements MethodBeforeAdvice {

        
    /* (non-Javadoc)
         * @see org.springframework.aop.MethodBeforeAdvice#before(java.lang.reflect.Method, java.lang.Object[], java.lang.Object)
         
    */

        
    public void before(Method m, Object[] args, Object target)
                throws Throwable 
    {
            String privilege
    =target.getClass().getName()+"." +m.getName();
            User user 
    = new User();
            
    if (!user.isPermission(privilege)) {
                
    throw new PermissionDeniedException(user, privilege);
            }

            System.
    out.println("Hello world! (by " + this.getClass().getName()+"::"
                    
    + target.getClass().getName()+"." +m.getName() +")");

        }


    }

    權限驗證異常:
    public class PermissionDeniedException extends Exception{
        
    public PermissionDeniedException(){
            super();
        }

        
    public PermissionDeniedException(User user,String pri){
            super();
        }

    }

    異常處理Advice
    public class PermissionThrowsAdvice implements ThrowsAdvice{
        
    public void afterThrowing(Method method, Object[] args, Object target,
                Throwable subclass) 
    {
            System.
    out.println("Logging that a " + subclass
                    
    + "Exception was thrown.");
        }


    }

    spring 配置文件:
    xml version="1.0" encoding="UTF-8"?>
    DOCTYPE beans PUBLIC  "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

    <beans>
      

      
    <bean id="bean" class="org.springframework.aop.framework.ProxyFactoryBean">
        
    <property name="proxyInterfaces">
          
    <value>com.jhalo.jsecurity.aop.ResourceBeanvalue>
        
    property>
        
    <property name="target">
          
    <ref local="beanTarget"/>
        
    property>
        
    <property name="interceptorNames">
          
    <list>
            
    <value>permissionCheckBeforeAdvisorvalue>
            
    <value>permissionThrowsAdvisorvalue>
          
    list>
        
    property>
      
    bean>
      
    <bean id="service" class="org.springframework.aop.framework.ProxyFactoryBean">
        
    <property name="proxyInterfaces">
          
    <value>com.jhalo.jsecurity.aop.Servicevalue>
        
    property>
        
    <property name="target">
          
    <ref local="serviceBean"/>
        
    property>
        
    <property name="interceptorNames">
          
    <list>
            
    <value>permissionCheckBeforeAdvisorvalue>
            
    <value>permissionThrowsAdvisorvalue>
          
    list>
        
    property>
      
    bean>

      

      
    <bean id="beanTarget" class="com.jhalo.jsecurity.aop.ResourceBeanImpl"/>
      
    <bean id="serviceBean" class="com.jhalo.jsecurity.aop.ServiceBean">
          
    <property name="bean">
            

            
    <ref local="bean"/>
          
    property>
      
    bean>
      

      

      
    <bean id="permissionCheckBeforeAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
        
    <property name="advice">
          
    <ref local="thePermissionCheckBeforeAdvice"/>
        
    property>
        
    <property name="pattern">
          

          
    <value>.*value>
        
    property>
      
    bean>
      

      
    <bean id="permissionThrowsAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
            
    <property name="advice">
                
    <ref local="thePermissionThrowsAdvice"/>
            
    property>
            
    <property name="pattern">
                
    <value>.*value>
            
    property>
      
    bean>

      

      
    <bean id="thePermissionCheckBeforeAdvice" class="com.jhalo.jsecurity.aop.PermissionCheckAdvice"/>
      
    <bean id="thePermissionThrowsAdvice" class="com.jhalo.jsecurity.aop.PermissionThrowsAdvice"/>
    beans>

    簡單測試:
    public class SpringAopTest {
        
    public static void main(String[] args) {
            
    //Read the configuration file
            ApplicationContext ctx
                
    = new FileSystemXmlApplicationContext("springconfig.xml");

            
    //Instantiate an object
            
    //ResourceBean x = (ResourceBean) ctx.getBean("bean");

            
    //Execute the public method of the bean (the test)
            
    //1
            
    //x.theMethod();
            
    //2
            String name = "";
            
    /*
            name = x.getMethod1();
            System.out.println("test result::" +name);
            name = x.getMethod2();
            System.out.println("test result::" +name);
            name = x.getMethod3();
            System.out.println("test result::" +name);
    */

            
            
    //3
            Service sb = (Service)ctx.getBean("service");
            
    try{
            name 
    = sb.getBeanInfo();
            }
    catch(PermissionDeniedException pde){}
            System.
    out.println("test result::" +name);
          }


    }

    下面是用戶在沒有調用方法2的權限時的運行結果:
    (support.DefaultListableBeanFactory  221 ) Pre-instantiating singletons in factory [org.springframework.beans.factory.support.DefaultListableBeanFactory defining beans [bean,service,beanTarget,serviceBean,permissionCheckBeforeAdvisor,permissionThrowsAdvisor,thePermissionCheckBeforeAdvice,thePermissionThrowsAdvice]; root of BeanFactory hierarchy]
    (support.DefaultListableBeanFactory  
    236 ) Creating shared instance of singleton bean 'bean'
    (core.CollectionFactory              
    55  ) Using JDK 1.4 collections
    (support.DefaultListableBeanFactory  
    236 ) Creating shared instance of singleton bean 'beanTarget'
    (support.DefaultListableBeanFactory  
    236 ) Creating shared instance of singleton bean 'permissionCheckBeforeAdvisor'
    (support.DefaultListableBeanFactory  
    236 ) Creating shared instance of singleton bean 'thePermissionCheckBeforeAdvice'
    (support.DefaultListableBeanFactory  
    236 ) Creating shared instance of singleton bean 'permissionThrowsAdvisor'
    (support.DefaultListableBeanFactory  
    236 ) Creating shared instance of singleton bean 'thePermissionThrowsAdvice'
    (support.DefaultListableBeanFactory  
    236 ) Creating shared instance of singleton bean 'service'
    (support.DefaultListableBeanFactory  
    236 ) Creating shared instance of singleton bean 'serviceBean'
    (adapter.ThrowsAdviceInterceptor     
    72  ) Found exception handler method [public void com.jhalo.jsecurity.aop.PermissionThrowsAdvice.afterThrowing(java.lang.reflect.Method,java.lang.Object[],java.lang.Object,java.lang.Throwable)]
    com.jhalo.jsecurity.aop.ServiceBean.getBeanInfo
    Hello world
    ! (by com.jhalo.jsecurity.aop.PermissionCheckAdvice::com.jhalo.jsecurity.aop.ServiceBean.getBeanInfo)
    (adapter.ThrowsAdviceInterceptor     
    72  ) Found exception handler method [public void com.jhalo.jsecurity.aop.PermissionThrowsAdvice.afterThrowing(java.lang.reflect.Method,java.lang.Object[],java.lang.Object,java.lang.Throwable)]
    com.jhalo.jsecurity.aop.ServiceBean.getBeanInfo
    com.jhalo.jsecurity.aop.ResourceBeanImpl.getMethod1
    Hello world
    ! (by com.jhalo.jsecurity.aop.PermissionCheckAdvice::com.jhalo.jsecurity.aop.ResourceBeanImpl.getMethod1)
    (adapter.ThrowsAdviceInterceptor     
    72  ) Found exception handler method [public void com.jhalo.jsecurity.aop.PermissionThrowsAdvice.afterThrowing(java.lang.reflect.Method,java.lang.Object[],java.lang.Object,java.lang.Throwable)]
    com.jhalo.jsecurity.aop.ServiceBean.getBeanInfo
    com.jhalo.jsecurity.aop.ResourceBeanImpl.getMethod1
    com.jhalo.jsecurity.aop.ResourceBeanImpl.getMethod3
    (adapter.ThrowsAdviceInterceptor     
    72  ) Found exception handler method [public void com.jhalo.jsecurity.aop.PermissionThrowsAdvice.afterThrowing(java.lang.reflect.Method,java.lang.Object[],java.lang.Object,java.lang.Throwable)]
    com.jhalo.jsecurity.aop.ServiceBean.getBeanInfo
    com.jhalo.jsecurity.aop.ResourceBeanImpl.getMethod1
    com.jhalo.jsecurity.aop.ResourceBeanImpl.getMethod3
    Hello world
    ! (by com.jhalo.jsecurity.aop.PermissionCheckAdvice::com.jhalo.jsecurity.aop.ResourceBeanImpl.getMethod3)
    test result::張三王五




    方向:分布式系統設計

    posted on 2005-03-31 16:51 java光環 閱讀(2092) 評論(0)  編輯  收藏 所屬分類: spring

    主站蜘蛛池模板: 亚洲高清偷拍一区二区三区| AV免费网址在线观看| 亚洲情a成黄在线观看| 小说专区亚洲春色校园| 成人午夜18免费看| 国产亚洲精品bv在线观看| 97无码免费人妻超级碰碰碰碰| 国产精品亚洲精品青青青| 国产v精品成人免费视频400条| 亚洲中字慕日产2021| 成年在线网站免费观看无广告| 亚洲人成网站色7799| 免费又黄又爽的视频| 一级日本高清视频免费观看| 国产亚洲3p无码一区二区| 午夜爽爽爽男女免费观看影院| 亚洲第一页中文字幕| 免费看韩国黄a片在线观看| 亚洲av无码专区在线电影 | 亚洲最大的视频网站| 亚洲最大免费视频网| 亚洲日韩一区二区三区| 亚洲 国产 图片| 久久免费精品视频| 亚洲一区二区三区写真| 亚洲精品视频免费| 91福利免费视频| 亚洲精品乱码久久久久久V| 亚洲午夜日韩高清一区| 亚洲精品在线免费观看视频| 激情无码亚洲一区二区三区 | 一本久久综合亚洲鲁鲁五月天| av午夜福利一片免费看久久| 91情国产l精品国产亚洲区| 性一交一乱一视频免费看| 国产日韩AV免费无码一区二区三区 | 免费无遮挡无码视频在线观看| 亚洲AV无码乱码国产麻豆穿越| 欧洲精品成人免费视频在线观看 | 九九精品成人免费国产片| 日本亚洲精品色婷婷在线影院 |