http://lighter.iteye.com/blog/42673
这一排在复习
spring的一些知识点,顺便写一下博客,下面看一下利用
spring AOP做的管理权限简单实例;
首先定义一个用户:
用户有三种人:未注册用户,注册用户,与管理员
注册用户可以可以发表,回复帖子
管理员除了可以发表,回复帖子,还可以删除帖子!
下面定义TestCommunity接口:
接下来,就是最重要的一个类,拦截器,Around处理类型的,类TestAuthorityInterceptor:
首先定义一个用户:
- publicclassUser{
- privateStringusername;
- publicStringgetUsername(){
- returnusername;
- }
- publicvoidsetUsername(Stringusername){
- this.username=username;
- }
- }
注册用户可以可以发表,回复帖子
管理员除了可以发表,回复帖子,还可以删除帖子!
下面定义TestCommunity接口:
- publicinterfaceTestCommunity{
- publicvoidanswerTopic();
- publicvoiddeleteTopic();
- }
实现上面接口的TestCommunityImpl类:
- publicclassTestCommunityImplimplementsTestCommunity{
- //注册用户与管理员拥有的功能
- publicvoidanswerTopic(){
- System.out.println("可以发表,回复帖子");
- }
- //管理员拥有的功能
- publicvoiddeleteTopic(){
- System.out.println("可以删除帖子!");
- }
- }
下一步,建立一下依赖注入的实现类TestResultImpl:
- publicclassTestResultImpl{
- privateTestCommunitytest;
- publicvoidsetTest(TestCommunitytest){
- this.test=test;
- }
- publicvoidanswerTopic()
- {
- test.answerTopic();
- }
- publicvoiddeleteTopic()
- {
- test.deleteTopic();
- }
- }
接下来,就是最重要的一个类,拦截器,Around处理类型的,类TestAuthorityInterceptor:
- importorg.aopalliance.intercept.MethodInterceptor;
- importorg.aopalliance.intercept.MethodInvocation;
- //创建Around处理应该实现MethodInterceptor接口
- publicclassTestAuthorityInterceptorimplementsMethodInterceptor{
- privateUseruser;
- publicUsergetUser(){
- returnuser;
- }
- publicvoidsetUser(Useruser){
- this.user=user;
- }
- //invoke方法返回调用的结果
- publicObjectinvoke(MethodInvocationinvocation)throwsThrowable{
- StringmethodName=invocation.getMethod().getName();
- if(user.getUsername().equals("unRegistedUser")){
- System.out.println("你的身份是未注册用户,没有权限回复,删除帖子!");
- returnnull;
- }
- if((user.getUsername().equals("user"))
- &&(methodName.equals("deleteTopic"))){
- System.out.println("你的身份是注册用户,没有权限删除帖子");
- returnnull;
- }
- //proceed()方法对连接点的整个拦截器链起作用,拦截器链中的每个拦截器都执行该方法,并返回它的返回值
- returninvocation.proceed();
- }
- }
配置文件:
- <?xmlversion="1.0"encoding="UTF-8"?>
- <!DOCTYPEbeansPUBLIC"-//SPRING//DTDBEAN//EN""http://www.springframework.org/dtd/spring-beans.dtd">
- <beans>
- <beanid="authTarget"class="org.test.lighter.TestCommunityImpl"/>
- <!--其中的username可以写为admin,user,和unRegistedUser-->
- <beanid="user"class="org.test.lighter.User">
- <propertyname="username"value="user"/>
- </bean>
- <!--配置拦截器-->
- <beanid="TestAuthorityInterceptor"
- class="org.test.lighter.TestAuthorityInterceptor">
- <propertyname="user"ref="user"/>
- </bean>
- <!--配置代理工厂bean-->
- <beanid="service"
- class="org.springframework.aop.framework.ProxyFactoryBean">
- <propertyname="proxyInterfaces">
- <value>org.test.lighter.TestCommunity</value>
- </property>
- <propertyname="target"ref="authTarget"/>
- <propertyname="interceptorNames">
- <list>
- <value>TestAuthorityInterceptor</value>
- </list>
- </property>
- </bean>
- <beanid="testResult"class="org.test.lighter.TestResultImpl">
- <propertyname="test"ref="service"/>
- </bean>
- </beans>
再写一个执行文件BeanTest:
- importorg.springframework.context.ApplicationContext;
- importorg.springframework.context.support.FileSystemXmlApplicationContext;
- publicclassBeanTest{
- publicstaticvoidmain(String[]args)throwsException
- {
- ApplicationContextctx=newFileSystemXmlApplicationContext("src/bean.xml");
- TestResultImpltest=(TestResultImpl)ctx.getBean("testResult");
- test.answerTopic();
- test.deleteTopic();
- }
- }
执行结果:大家猜一下啦
- 1、如果是管理员,打印出:
- 可以发表,回复帖子
- 可以删除帖子!
- 2、如果是注册用户:
- 可以发表,回复帖子
- 你的身份是注册用户,没有权限删除帖子
- 3、未注册用户:
- 你的身份是未注册用户,没有权限回复,删除帖子!
Spring AOP 权限管理示例
本文介绍了一个使用 Spring AOP 实现的简单权限管理系统。通过定义用户角色及功能接口,配合自定义拦截器,实现了不同用户级别的权限控制。具体展示了如何针对未注册用户、普通用户和管理员设置不同的操作权限。


137

被折叠的 条评论
为什么被折叠?



