用spring Aop 做的管理許可權(簡單例項)
阿新 • • 發佈:2019-01-01
首先定義一個使用者:
Java程式碼
- public class User {
- private String username;
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- }
使用者有三種人:未註冊使用者,註冊使用者,與管理員
註冊使用者可以可以發表,回覆帖子
管理員除了可以發表,回覆帖子,還可以刪除帖子!
下面定義TestCommunity介面:
Java程式碼
- public interface TestCommunity {
- public void answerTopic();
- public void deleteTopic();
- }
實現上面介面的TestCommunityImpl類:
Java程式碼
- public class TestCommunityImpl implements TestCommunity {
- //註冊使用者與管理員擁有的功能
- public void answerTopic() {
-
System.out.println("可以發表,回覆帖子"
- }
- //管理員擁有的功能
- public void deleteTopic() {
- System.out.println("可以刪除帖子!");
- }
- }
下一步,建立一下依賴注入的實現類TestResultImpl:
Java程式碼
- public class TestResultImpl {
- private TestCommunity test;
- public void setTest(TestCommunity test) {
-
this.test = test;
- }
- public void answerTopic()
- {
- test.answerTopic();
- }
- public void deleteTopic()
- {
- test.deleteTopic();
- }
- }
接下來,就是最重要的一個類,攔截器,Around處理型別的,類TestAuthorityInterceptor:
Java程式碼
- import org.aopalliance.intercept.MethodInterceptor;
- import org.aopalliance.intercept.MethodInvocation;
- //建立Around處理應該實現MethodInterceptor介面
- public class TestAuthorityInterceptor implements MethodInterceptor {
- private User user;
- public User getUser() {
- return user;
- }
- public void setUser(User user) {
- this.user = user;
- }
- // invoke方法返回呼叫的結果
- public Object invoke(MethodInvocation invocation) throws Throwable {
- String methodName = invocation.getMethod().getName();
- if (user.getUsername().equals("unRegistedUser")) {
- System.out.println("你的身份是未註冊使用者,沒有許可權回覆,刪除帖子!");
- return null;
- }
- if ((user.getUsername().equals("user"))
- && (methodName.equals("deleteTopic"))) {
- System.out.println("你的身份是註冊使用者,沒有許可權刪除帖子");
- return null;
- }
- // proceed()方法對連線點的整個攔截器鏈起作用,攔截器鏈中的每個攔截器都執行該方法,並返回它的返回值
- return invocation.proceed();
- }
- }
配置檔案:
Java程式碼
- <?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="authTarget" class="org.test.lighter.TestCommunityImpl" />
- <!-- 其中的username可以寫為admin,user,和unRegistedUser -->
- <bean id="user" class="org.test.lighter.User">
- <property name="username" value="user" />
- </bean>
- <!-- 配置攔截器 -->
- <bean id="TestAuthorityInterceptor"
- class="org.test.lighter.TestAuthorityInterceptor">
- <property name="user" ref="user" />
- </bean>
- <!-- 配置代理工廠bean -->
- <bean id="service"
- class="org.springframework.aop.framework.ProxyFactoryBean">
- <property name="proxyInterfaces">
- <value>org.test.lighter.TestCommunity</value>
- </property>
- <property name="target" ref="authTarget"/>
- <property name="interceptorNames">
- <list>
- <value>TestAuthorityInterceptor</value>
- </list>
- </property>
- </bean>
- <bean id="testResult" class="org.test.lighter.TestResultImpl">
- <property name="test" ref="service" />
- </bean>
- </beans>
再寫一個執行檔案BeanTest:
Java程式碼
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.FileSystemXmlApplicationContext;
- public class BeanTest {
- public static void main(String[] args) throws Exception
- {
- ApplicationContext ctx = new FileSystemXmlApplicationContext("src/bean.xml");
- TestResultImpl test = (TestResultImpl)ctx.getBean("testResult");
- test.answerTopic();
- test.deleteTopic();
- }
- }
執行結果:大家猜一下啦
Java程式碼
- 1、如果是管理員,打印出:
- 可以發表,回覆帖子
- 可以刪除帖子!
- 2、如果是註冊使用者:
- 可以發表,回覆帖子
- 你的身份是註冊使用者,沒有許可權刪除帖子
- 3、未註冊使用者:
- 你的身份是未註冊使用者,沒有許可權回覆,刪除帖子!