`
meiowei
  • 浏览: 146193 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

利用spring AOP管理权限

阅读更多
利用spring AOP做的管理权限简单实例;
首先定义一个用户:
Java代码
public class User {  
    private String username;  
 
    public String getUsername() {  
        return username;  
    }  
    public void setUsername(String username) {  
        this.username = username;  
    }  


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();  


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("可以删除帖子!");  
    }  


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();  
      }  


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();  
    }  
 


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> 

<?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();  
  }  


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、未注册用户:  
你的身份是未注册用户,没有权限回复,删除帖子!
分享到:
评论

相关推荐

    ssm_traveller:基于ssm的旅游后台管理系统,使用AdminLTE 前端框架,实现基本的CRUD功能,使用spring-security进行用户登录权限控制,利用spring aop记录访问日志

    旅游后台管理系统 1、项目环境 IDEA 2018.2 Mysql 5.5.27 jdk1.8 tomcat7 2、如何启动 1、在启动位置,配置maven,模块为ssm_web,使用tomcat7:run启动 2、网址: ​ 用户:admin 密码:123

    Spring入门

    2、轻松实现如权限拦截、运行期监控等功能:利用Spring提供了AOP技术。 3、 提供单例模式支持,开发人员不再需要自己编写实现代码 4、Spring提供了众多服务。如:事务管理,编程人员不再需要手工控制事务,也不需...

    SpringBoot集成常用开发中间件,分库分表,缓存,消息队列,定时器,权限管理等组件.zip

    springboot Spring框架是Java平台上的一种开源应用框架,提供具有控制反转...Spring框架具有面向切面编程(AOP)框架,SpringAOP框架基于代理模式,同时运行时可配置;AOP框架主要针对模块之间的交叉关注点进行模块化。

    Springboot + shiro权限管理 这或许是流程最详细、代码最干净、配置最简单的shiro上手项目了 .zip

    springboot Spring框架是Java平台上的一种开源应用框架,提供具有控制反转...Spring框架具有面向切面编程(AOP)框架,SpringAOP框架基于代理模式,同时运行时可配置;AOP框架主要针对模块之间的交叉关注点进行模块化。

    shiro集成hibernate和spring

    之后就可以在Controller上面利用注解比如: @RequiresPermissions(value = { "user:select" }) 当然也可以利用Spring的aop也是一样呢 假如发现问题可以联系本人QQ134266212 或者邮箱noatnu@163.com

    Spring面试题

    4.容器提供了AOP技术,利用它很容易实现如权限拦截,运行期监控等功能 5.容器提供了众多的辅助类,能加快应用的开发 6.spring对于主流的应用框架提供了集成支持,如hibernate,JPA,Struts等 7.spring属于低侵入...

    spring笔记

    (4)容器提供了AOP技术,利用它很容易实现如权限拦截,运行期监控等功能。 (5)容器提供的众多辅作类,使用这些类能够加快应用的开发,如:JdbcTemplate、HibernateTemplate. (6)对主流的应用框架提供了集成支持...

    Spring-Boot-Web-Manamgement

    基于Spring Boot的权限后台管理系统新增功能:1.cron表达式转换器:通过前端界面自动生成cron表达式,方便我们后续创建定时任务时所需要添加的执行时间2.定时任务:开启了多端口多线程执行的定时任务可以执行停止...

    基于ssm的旅游后台管理系统.zip

    使用AdminLTE 前端框架,实现基本的CRUD功能,使用spring-security进行用户登录权限控制,利用spring aop记录访问日志 毕业设计是高等教育阶段学生在完成学业前所进行的一项重要学术任务,旨在检验学生通过学习所...

    springboot+spring security+JWT+mybatisplus

    用springboot集成security,做用户验证和权限管理;JWT认证token;利用Aop做操作日志; 用mybatisplus 实现代码自动生成工具类。用swagger实现接口文档

    JAVA制造装备物联及生产管理(ERP)系统源码

    Spring(IOC DI AOP 声明式事务处理) SpringMVC(支持Restful风格) Hibernate Validator(参数校验) Mybatis(最少配置方案) shiro权限控制,结合ajax实现了异步认证与异步授权,同时实现了细粒度的...

    基于字典树的大学生交流 社区的设计与实现

    实现敏感词过滤时用到了字典树算法,在实现热点排序的时候用到了权值计算,同时利用Redis缓存实现点赞和关注,利用Kafka实现系统通知,并且采用AOP统一进行了状态管理,异常处理,而Spring Security 实现了权限控制...

    基于SSM框架的生产管理ERP系统源码+数据库+项目文档,含计划进度、设备管理、工艺监控、物料监控、人员监控、质量监控、系统管理

    Spring(IOC DI AOP 声明式事务处理) SpringMVC(支持Restful风格) Hibernate Validator(参数校验) Mybatis(最少配置方案) shiro权限控制,结合ajax实现了异步认证与异步授权,同时实现了细粒度的权限动态...

    production_ssm-master.rar

    Spring(IOC DI AOP 声明式事务处理) SpringMVC(支持Restful风格) Hibernate Validator(参数校验) Mybatis(最少配置方案) shiro权限控制,结合ajax实现了异步认证与异步授权,同时实现了细粒度的权限动态...

    java ssm开发的生产管理ERP系统

    - Spring(IOC DI AOP 声明式事务处理) - SpringMVC(支持Restful风格) - Hibernate Validator(参数校验) - Mybatis(最少配置方案) - shiro权限控制,结合ajax实现了异步认证与异步授权,同时实现了细粒度的...

    基于微信小程序的家庭事务管理微信小程序的实现+ssm框架.rar

    利用Spring事务管理确保了数据库操作的一致性和完整性,保证了系统的稳定性和可靠性。 家庭事务管理功能: 用户可以创建家庭组织,邀请家庭成员加入,并管理家庭成员的权限和角色。 提供任务管理功能,包括任务的...

    Crm客户关系管理系统

    利用相应的信息技术以及互联网技术来协调企业与顾客间在销售、营销和服务上的交互,从而提升其管理方式,向客户提供创新式的个性化的客户交互和服务,系统提供营销管理、客户管理、用户管理、角色管理、模块管理、...

    weixin135房屋租赁管理系统的设计与实现+ssm--论文pf.rar

    Spring:一个开源的轻量级Java SE/Java EE全功能栈框架,提供了IoC容器和AOP功能,可以管理对象的生命周期和声明式事务。 Spring MVC:作为Spring的一个模块,提供了构建Web应用程序的MVC(模型-视图-控制器)设计...

    MF00331-JAVA制造装备物联生产管理(ERP)源码.zip

    Spring(IOC DI AOP 声明式事务处理) SpringMVC(支持Restful风格) Hibernate Validator(参数校验) Mybatis(最少配置方案) shiro权限控制,结合ajax实现了异步认证与异步授权,同时实现了细粒度的权限动态...

    数据采集系统

    利用aop + scheduler 采用动态表实现日志管理,提高日志表的存储和访问能力. .集成 spring cache ,对 service层用户参与调查进行缓存处理,有效降低数据库负 载压力. 0.使用 RoutingDatasource + token对数据源路由...

Global site tag (gtag.js) - Google Analytics