1. 程式人生 > >用spring Aop 做的管理許可權(簡單例項)

用spring Aop 做的管理許可權(簡單例項)


首先定義一個使用者: 
Java程式碼  收藏程式碼
  1. public class User {  
  2.     private String username;  
  3.     public String getUsername() {  
  4.         return username;  
  5.     }  
  6.     public void setUsername(String username) {  
  7.         this.username = username;  
  8.     }  
  9. }  

使用者有三種人:未註冊使用者,註冊使用者,與管理員 
註冊使用者可以可以發表,回覆帖子 
管理員除了可以發表,回覆帖子,還可以刪除帖子! 

下面定義TestCommunity介面: 
Java程式碼  收藏程式碼
  1. public interface TestCommunity {  
  2.   public void answerTopic();  
  3.   public void deleteTopic();  
  4. }  


實現上面介面的TestCommunityImpl類: 
Java程式碼  收藏程式碼
  1. public class TestCommunityImpl implements TestCommunity {  
  2.     //註冊使用者與管理員擁有的功能  
  3.     public void answerTopic() {  
  4.         System.out.println("可以發表,回覆帖子"
    );  
  5.     }  
  6.     //管理員擁有的功能  
  7.     public void deleteTopic() {  
  8.         System.out.println("可以刪除帖子!");  
  9.     }  
  10. }  


下一步,建立一下依賴注入的實現類TestResultImpl: 
Java程式碼  收藏程式碼
  1. public class TestResultImpl {  
  2.     private TestCommunity test;  
  3.     public void setTest(TestCommunity test) {  
  4.         this.test = test;  
  5.     }     
  6.      public void answerTopic()  
  7.      {  
  8.          test.answerTopic();  
  9.      }  
  10.       public void deleteTopic()  
  11.       {  
  12.           test.deleteTopic();  
  13.       }  
  14. }  


接下來,就是最重要的一個類,攔截器,Around處理型別的,類TestAuthorityInterceptor: 
Java程式碼  收藏程式碼
  1. import org.aopalliance.intercept.MethodInterceptor;  
  2. import org.aopalliance.intercept.MethodInvocation;  
  3. //建立Around處理應該實現MethodInterceptor介面  
  4. public class TestAuthorityInterceptor implements MethodInterceptor {  
  5.     private User user;  
  6.     public User getUser() {  
  7.         return user;  
  8.     }  
  9.     public void setUser(User user) {  
  10.         this.user = user;  
  11.     }  
  12.     // invoke方法返回呼叫的結果  
  13.     public Object invoke(MethodInvocation invocation) throws Throwable {  
  14.         String methodName = invocation.getMethod().getName();  
  15.         if (user.getUsername().equals("unRegistedUser")) {  
  16.             System.out.println("你的身份是未註冊使用者,沒有許可權回覆,刪除帖子!");  
  17.             return null;  
  18.         }  
  19.         if ((user.getUsername().equals("user"))  
  20.                 && (methodName.equals("deleteTopic"))) {  
  21.             System.out.println("你的身份是註冊使用者,沒有許可權刪除帖子");  
  22.             return null;  
  23.         }  
  24.         // proceed()方法對連線點的整個攔截器鏈起作用,攔截器鏈中的每個攔截器都執行該方法,並返回它的返回值  
  25.         return invocation.proceed();  
  26.     }  
  27. }  


配置檔案: 
Java程式碼  收藏程式碼
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">  
  3. <beans>  
  4.     <bean id="authTarget" class="org.test.lighter.TestCommunityImpl" />  
  5.     <!-- 其中的username可以寫為admin,user,和unRegistedUser -->  
  6.     <bean id="user" class="org.test.lighter.User">  
  7.         <property name="username" value="user" />  
  8.     </bean>  
  9.     <!-- 配置攔截器 -->  
  10.     <bean id="TestAuthorityInterceptor"  
  11.         class="org.test.lighter.TestAuthorityInterceptor">  
  12.         <property name="user" ref="user" />  
  13.     </bean>  
  14.     <!-- 配置代理工廠bean -->  
  15.     <bean id="service"  
  16.         class="org.springframework.aop.framework.ProxyFactoryBean">  
  17.         <property name="proxyInterfaces">  
  18.             <value>org.test.lighter.TestCommunity</value>  
  19.         </property>  
  20.         <property name="target" ref="authTarget"/>  
  21.         <property name="interceptorNames">  
  22.             <list>  
  23.                 <value>TestAuthorityInterceptor</value>  
  24.             </list>  
  25.         </property>  
  26.     </bean>  
  27.     <bean id="testResult" class="org.test.lighter.TestResultImpl">  
  28.         <property name="test" ref="service" />  
  29.     </bean>  
  30. </beans>  


再寫一個執行檔案BeanTest: 
Java程式碼  收藏程式碼
  1. import org.springframework.context.ApplicationContext;  
  2. import org.springframework.context.support.FileSystemXmlApplicationContext;  
  3. public class BeanTest {  
  4.   public static void main(String[] args) throws Exception  
  5.   {  
  6.       ApplicationContext ctx = new FileSystemXmlApplicationContext("src/bean.xml");  
  7.       TestResultImpl test = (TestResultImpl)ctx.getBean("testResult");  
  8.       test.answerTopic();  
  9.       test.deleteTopic();  
  10.   }  
  11. }  


執行結果:大家猜一下啦 
Java程式碼  收藏程式碼
  1. 1、如果是管理員,打印出:  
  2. 可以發表,回覆帖子  
  3. 可以刪除帖子!  
  4. 2、如果是註冊使用者:  
  5. 可以發表,回覆帖子  
  6. 你的身份是註冊使用者,沒有許可權刪除帖子  
  7. 3、未註冊使用者:  
  8. 你的身份是未註冊使用者,沒有許可權回覆,刪除帖子!