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

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

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

    andyj2ee

    java tec sky

    統(tǒng)計(jì)

    留言簿(4)

    activemq

    aop

    design pattern

    other blog

    spring

    workflow

    多線程

    軟件架構(gòu)師

    閱讀排行榜

    評(píng)論排行榜

    基于spring aop 權(quán)限管理系統(tǒng)原型<1>

    111-spring_aop.gif

    此權(quán)限管理系統(tǒng)把待訪問的業(yè)務(wù)層方法做為權(quán)限管理中的資源,通過spring aop 對(duì)接口方法進(jìn)行攔截,來實(shí)現(xiàn)權(quán)限的管理,可以實(shí)現(xiàn)細(xì)粒度的權(quán)限控制。
    初步采用捕獲權(quán)限否決異常實(shí)現(xiàn)。代碼如下:
    資源接口:

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

    資源實(shí)現(xiàn)類
    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 "王五";
        }


    }

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

    服務(wù)層接口實(shí)現(xiàn)類:
    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;
        }


    }

    用戶權(quán)限類:
    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;
        }


    }


    權(quán)限驗(yàn)證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() +")");

        }


    }

    權(quán)限驗(yàn)證異常:
    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);
          }


    }

    下面是用戶在沒有調(diào)用方法2的權(quán)限時(shí)的運(yùn)行結(jié)果:
    (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::張三王五




    方向:分布式系統(tǒng)設(shè)計(jì)

    posted on 2005-03-31 16:51 java光環(huán) 閱讀(2083) 評(píng)論(0)  編輯  收藏 所屬分類: spring

    主站蜘蛛池模板: 亚洲香蕉在线观看| 亚洲国产高清视频| 亚洲中文字幕无码一久久区| 国产精品亚洲αv天堂无码 | 久久青青草原亚洲AV无码麻豆 | 无码成A毛片免费| 亚洲精品欧美综合四区| 亚洲aⅴ无码专区在线观看春色| 美女被羞羞网站免费下载| 亚洲精品第一国产综合野| 亚洲AV无码片一区二区三区| 日本精品久久久久久久久免费| 免费福利在线视频| 抽搐一进一出gif免费视频| 免费福利在线观看| 免费国产午夜高清在线视频| 麻豆最新国产剧情AV原创免费| 国产精品va无码免费麻豆| 久久亚洲AV无码西西人体| 亚洲国产精品一区二区第四页| 国产青草视频免费观看97| 亚洲一区无码中文字幕| 免费又黄又硬又爽大片| 永久黄网站色视频免费| 亚洲色精品vr一区二区三区| 亚洲人成电影院在线观看| 亚洲国产成人精品电影| 免费看又黄又爽又猛的视频软件| 免费人成黄页在线观看日本| 在线观看人成网站深夜免费| 国产亚洲综合久久系列| 亚洲色大成网站www永久男同| 99亚偷拍自图区亚洲| 亚洲另类自拍丝袜第五页| 亚洲JLZZJLZZ少妇| 永久在线观看免费视频| 无码成A毛片免费| 夜色阁亚洲一区二区三区| 波多野结衣中文一区二区免费 | 国产卡二卡三卡四卡免费网址| 免费大黄网站在线观看|