Spring Aop 權限

TestServicespring

  
  
  
  
  1. package com.zbq.authorize; 
  2.  
  3. public interface TestService { 
  4.     public void view(); 
  5.     public void modify(); 

TestServiceImplapp

  
  
  
  
  1. package com.zbq.authorize; 
  2.  
  3. public class TestServiceImpl implements TestService { 
  4.  
  5.     public void modify() { 
  6.         System.out.println("用戶修改數據"); 
  7.     } 
  8.  
  9.     public void view() { 
  10.         System.out.println("用戶查看數據"); 
  11.     } 
  12.  

TestActionide

  
  
  
  
  1. package com.zbq.authorize; 
  2.  
  3. public interface TestAction { 
  4.     public void view(); 
  5.     public void modify(); 

TestActionImplthis

  
  
  
  
  1. package com.zbq.authorize; 
  2.  
  3. public class TestActiomImpl implements TestAction { 
  4.     private TestService ts; 
  5.      
  6.     public TestService getTs() { 
  7.         return ts; 
  8.     } 
  9.  
  10.     public void setTs(TestService ts) { 
  11.         this.ts = ts; 
  12.     } 
  13.  
  14.     public void modify() { 
  15.         ts.modify(); 
  16.     } 
  17.  
  18.     public void view() { 
  19.         ts.view(); 
  20.     } 
  21.  

AuthorityInterceptorspa

  
  
  
  
  1. package com.zbq.authorize; 
  2.  
  3. import org.aopalliance.intercept.MethodInterceptor; 
  4. import org.aopalliance.intercept.MethodInvocation; 
  5.  
  6. public class AuthorityInterceptor implements MethodInterceptor { 
  7.     private String user; 
  8.      
  9.     public String getUser() { 
  10.         return user; 
  11.     } 
  12.  
  13.     public void setUser(String user) { 
  14.         this.user = user; 
  15.     } 
  16.  
  17.     public Object invoke(MethodInvocation invocation) throws Throwable { 
  18.         System.out.println("-----------------"); 
  19.         String methodName = invocation.getMethod().getName(); 
  20.          
  21.         if(!("admin".equals(user)) && !("registedUser".equals(user))){ 
  22.             System.out.println("您無權執行該方法"); 
  23.             return null
  24.         }else if(("registedUser".equals(user)) && ("modify".equals(methodName))){ 
  25.             System.out.println("您不是管理員,沒法修改數據"); 
  26.             return null
  27.         }else { 
  28.             return invocation.proceed(); 
  29.         } 
  30.     } 
  31.  

applicationContext.xmlxml

  
  
  
  
  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <beans 
  3.     xmlns="http://www.springframework.org/schema/beans" 
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  5.     xmlns:p="http://www.springframework.org/schema/p" 
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> 
  7.  
  8.     <bean id="serviceTarget" class="com.zbq.authorize.TestServiceImpl"/> 
  9.      
  10.     <bean id="authorityInterceptor" class="com.zbq.authorize.AuthorityInterceptor"> 
  11.         <property name="user"> 
  12.             <value>registedUser</value> 
  13.         </property> 
  14.     </bean>  
  15.      
  16.     <bean id="service" class="org.springframework.aop.framework.ProxyFactoryBean"> 
  17.         <property name="proxyInterfaces"> 
  18.             <value>com.zbq.authorize.TestService</value> 
  19.         </property> 
  20.          
  21.         <property name="target"> 
  22.             <ref local="serviceTarget"/> 
  23.         </property> 
  24.          
  25.         <property name="interceptorNames"> 
  26.             <list> 
  27.                 <value>authorityInterceptor</value> 
  28.             </list> 
  29.         </property> 
  30.     </bean> 
  31.      
  32.     <bean id="testAction" class="com.zbq.authorize.TestActiomImpl"> 
  33.         <property name="ts"> 
  34.             <ref local="service"/> 
  35.         </property> 
  36.     </bean> 
  37. </beans> 

Clientget

  
  
  
  
  1. package com.zbq.authorize; 
  2.  
  3. import org.springframework.beans.factory.xml.XmlBeanFactory; 
  4. import org.springframework.core.io.ClassPathResource; 
  5.  
  6. public class Client { 
  7.  
  8.     public static void main(String[] args) { 
  9.         ClassPathResource res = new ClassPathResource("applicationContext.xml"); 
  10.         XmlBeanFactory factory = new XmlBeanFactory(res); 
  11.          
  12.         TestAction ta = (TestAction) factory.getBean("testAction"); 
  13.         ta.view(); 
  14.         ta.modify(); 
  15.     } 
  16.  
相關文章
相關標籤/搜索