此权限管理系统把待访问的业务层方法做为权限管理中的资源,通过spring aop 对接口方法进行拦截,来实现权限的管理,可以实现细粒度的权限控制。

在上文体验了spring aop 一些特性,aop 接口:MethodBeforeAdvice, AfterReturningAdvice, ThrowsAdvice 实现这三个接口分别对方法执行前,后,执行中抛异常等情况进行的,我们要是想做overload 这样的操作时,要用MethodInterceptor接口,此接口好在有返回值,

publicObject invoke(

      MethodInvocation invocation)

      throws Throwable

{

//.}

上文做法有些牵强业务逻辑还有throws PermissionDeniedException 感觉不爽,现在用MethodInterceptor 接口,来写这个demo,把权限与业务分开。

advice 如下:

publicclassPermissionCheckAroundAdvice implements MethodInterceptor{

    SecurityManager securityMgr=newSecurityManager();

/**//**

     * @param securityMgr The securityMgr to set.

*/

publicvoidsetSecurityMgr(SecurityManager securityMgr){

this.securityMgr=securityMgr;

    }

publicObject invoke(MethodInvocation invocation) throws Throwable{

        System.out.println("(被调用方法接口类名:"+invocation.getMethod().getDeclaringClass().getName()+")");

        System.out.println("(被调用方法名:"+invocation.getMethod().getName()+")");

        String methodName=invocation.getMethod().getDeclaringClass()

                .getName()+"."+invocation.getMethod().getName();

        System.out.println("(被调用方法全名:"+methodName+")");

        System.out.println("有否权限:("+securityMgr.checkPermission(methodName)+")");

if(securityMgr.checkPermission(methodName))

returninvocation.proceed();

         System.out.println("Goodbye! NO Permission!(by"+this.getClass().getName()+")");

return"--";

    }}

服务层业务接口修改如下:

publicinterfaceService{

publicString getBeanInfo();

}

服务层业务实现类如下:

publicclassServiceBean implements Service{

    ResourceBean bean;

/**//**

     * @param bean The bean to set.

*/

publicvoidsetBean(ResourceBean bean){

this.bean=bean;

    }

publicString getBeanInfo(){

        String result="";

        result+=bean.getMethod1();

        result+=bean.getMethod2();

        result+=bean.getMethod3();

returnresult;

    }

}

资源层,接口 ,类如下:

publicinterfaceResource{

}

publicinterfaceResourceBean extends Resource{

publicvoidtheMethod();

publicString getMethod1();

publicString getMethod2();

publicString getMethod3();

}

publicclassResourceBeanImpl implements ResourceBean,InitializingBean{

publicvoidtheMethod(){

        System.out.println(this.getClass().getName()

+"."+newException().getStackTrace()[0].getMethodName()

+"()"+"says HELLO!");

    }

publicString getMethod1(){

return"张三";

    }

publicString getMethod2(){

return"李四";

    }

publicString getMethod3(){

return"王五";

    }

publicvoidafterPropertiesSet() throws Exception{

        System.out.println("事件监听:类ResourceBeanImpl属性设置完毕");

    }

}

权限管理类:

publicclassUser{

    List privilages=newjava.util.ArrayList();

    String name;

publicUser(){

    }

/**//**

     * @param privilages The privilages to set.

*/

publicvoidsetPrivilages(List privilages){

this.privilages=privilages;

    }

publicString getName(){

returnname;

    }

publicvoidsetName(String name){

this.name=name;

    }

publicboolean isPermission(String pri){

        java.util.Iterator it=privilages.iterator();

        String p="";

        boolean pass=false;

while(it.hasNext()){

            p=(String)it.next();

            System.out.println(p);

if(p.equals(pri)){

                pass=true;

break;

            }        }returnpass;

    }

}

publicclassSecurityManager{

    User user;

publicvoidsetUser(User user){

this.user=user;

    }

publicboolean checkPermission(String privilege){

returncheckPermission(user,privilege);

    }

publicboolean checkPermission(User user, String privilege){

returnuser.isPermission(privilege);

    }

}

配置文件:

<?xml  version="1.0" encoding="UTF-8"?>beans PUBLIC  "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

com.jhalo.jsecurity.aop.ResourceBeanpermissionAroundAdvisorcom.jhalo.jsecurity.aop.ServicepermissionAroundAdvisor

testercom.jhalo.jsecurity.aop.ResourceBean.getMethod3com.jhalo.jsecurity.aop.Service.getBeanInfocom.jhalo.jsecurity.aop.ResourceBean.getMethod1

.*

User 所拥有的权限是在spring 配置文件中手工配置的,在实际应用中不可行,可以从DB中取得。

测试类:

publicclassSpringAopTest{

publicstaticvoidmain(String[] args){

//Read the configuration fileApplicationContext ctx

=newFileSystemXmlApplicationContext("springconfig.xml");

String name="";

Service sb=(Service)ctx.getBean("service");

//System.out.println("---"+ctx.isSingleton("service")+"---");name=sb.getBeanInfo();

        System.out.println("test result::"+name);

      }

}

测试结果 :

(xml.XmlBeanDefinitionReader119) Loading XML bean definitions from file [D:\projects\actives\jsecurity\springconfig.xml]

(support.FileSystemXmlApplicationContext90) Bean factoryforapplication context [org.springframework.context.support.FileSystemXmlApplicationContext;hashCode=25853693]: org.springframework.beans.factory.support.DefaultListableBeanFactory defining beans [bean,service,resourceMgr,beanTarget,beanTarget2,user,securityMgr,serviceBean,permissionAroundAdvisor,thePermissionCheckBeforeAdvice,thePermissionThrowsAdvice,thePermissionAroundAdvice]; root of BeanFactory hierarchy

(support.FileSystemXmlApplicationContext287)12beans definedinapplication context [org.springframework.context.support.FileSystemXmlApplicationContext;hashCode=25853693]

(support.FileSystemXmlApplicationContext395) Unable to locate MessageSource with name'messageSource':usingdefault[org.springframework.context.support.StaticMessageSource:{}]

(support.FileSystemXmlApplicationContext417) Unable to locate ApplicationEventMulticaster with name'applicationEventMulticaster':usingdefault[org.springframework.context.event.SimpleApplicationEventMulticaster@5e5a50]

(support.FileSystemXmlApplicationContext439) Refreshing listeners

(support.DefaultListableBeanFactory236) Creating shared instance of singleton bean'resourceMgr'(support.FileSystemXmlApplicationContext448) Application listener [com.jhalo.jsecurity.aop.ResourceManager@a3d4cf] added

(support.DefaultListableBeanFactory221) Pre-instantiating singletonsinfactory [org.springframework.beans.factory.support.DefaultListableBeanFactory defining beans [bean,service,resourceMgr,beanTarget,beanTarget2,user,securityMgr,serviceBean,permissionAroundAdvisor,thePermissionCheckBeforeAdvice,thePermissionThrowsAdvice,thePermissionAroundAdvice]; root of BeanFactory hierarchy]

(support.DefaultListableBeanFactory236) Creating shared instance of singleton bean'bean'(core.CollectionFactory55) Using JDK1.4collections

(support.DefaultListableBeanFactory236) Creating shared instance of singleton bean'beanTarget'事件监听:类ResourceBeanImpl属性设置完毕

(support.DefaultListableBeanFactory236) Creating shared instance of singleton bean'permissionAroundAdvisor'(support.DefaultListableBeanFactory236) Creating shared instance of singleton bean'thePermissionAroundAdvice'(support.DefaultListableBeanFactory236) Creating shared instance of singleton bean'securityMgr'(support.DefaultListableBeanFactory236) Creating shared instance of singleton bean'user'(support.DefaultListableBeanFactory236) Creating shared instance of singleton bean'service'(support.DefaultListableBeanFactory236) Creating shared instance of singleton bean'serviceBean'(support.DefaultListableBeanFactory236) Creating shared instance of singleton bean'beanTarget2'事件监听:类ResourceBean2Impl属性设置完毕

(support.DefaultListableBeanFactory236) Creating shared instance of singleton bean'thePermissionCheckBeforeAdvice'(support.DefaultListableBeanFactory236) Creating shared instance of singleton bean'thePermissionThrowsAdvice'--------ContextRefreshedEvent called

(被调用方法接口类名: com.jhalo.jsecurity.aop.Service)

(被调用方法名:getBeanInfo)

(被调用方法全名:com.jhalo.jsecurity.aop.Service.getBeanInfo)

com.jhalo.jsecurity.aop.ResourceBean.getMethod3

com.jhalo.jsecurity.aop.Service.getBeanInfo

有否权限:(true)

com.jhalo.jsecurity.aop.ResourceBean.getMethod3

com.jhalo.jsecurity.aop.Service.getBeanInfo

(被调用方法接口类名: com.jhalo.jsecurity.aop.ResourceBean)

(被调用方法名:getMethod1)

(被调用方法全名:com.jhalo.jsecurity.aop.ResourceBean.getMethod1)

com.jhalo.jsecurity.aop.ResourceBean.getMethod3

com.jhalo.jsecurity.aop.Service.getBeanInfo

com.jhalo.jsecurity.aop.ResourceBean.getMethod1

有否权限:(true)

com.jhalo.jsecurity.aop.ResourceBean.getMethod3

com.jhalo.jsecurity.aop.Service.getBeanInfo

com.jhalo.jsecurity.aop.ResourceBean.getMethod1

(被调用方法接口类名: com.jhalo.jsecurity.aop.ResourceBean)

(被调用方法名:getMethod2)

(被调用方法全名:com.jhalo.jsecurity.aop.ResourceBean.getMethod2)

com.jhalo.jsecurity.aop.ResourceBean.getMethod3

com.jhalo.jsecurity.aop.Service.getBeanInfo

com.jhalo.jsecurity.aop.ResourceBean.getMethod1

有否权限:(false)

com.jhalo.jsecurity.aop.ResourceBean.getMethod3

com.jhalo.jsecurity.aop.Service.getBeanInfo

com.jhalo.jsecurity.aop.ResourceBean.getMethod1

Goodbye!NO Permission!(by com.jhalo.jsecurity.aop.PermissionCheckAroundAdvice)

(被调用方法接口类名: com.jhalo.jsecurity.aop.ResourceBean)

(被调用方法名:getMethod3)

(被调用方法全名:com.jhalo.jsecurity.aop.ResourceBean.getMethod3)

com.jhalo.jsecurity.aop.ResourceBean.getMethod3

有否权限:(true)

com.jhalo.jsecurity.aop.ResourceBean.getMethod3

test result::张三--王五

这样就完全把企业业务逻辑与权限管理系统分开了,服务层,资源层与权限检查分离,用spring aop 通过配置文件把它们粘合在一起。实现了细粒度(对资源层数据)的权限检查。

接下来在此权限管理系统中引用角色概念,向 rbac 系统进军.(未完待续)

参考资料:

An Introduction to Aspect-Oriented Programming with the Spring Framework, Part 1 by Russell Miles -- The Spring framework, which supports development of the different facets of J2EE, provides an aspect-oriented programming module that gives Spring developers the opportunity to apply aspects to their applications. This article shows you how to work with AOP in Spring.

An Introduction to Aspect-Oriented Programming with the Spring Framework, Part 2 by Russell Miles -- Russ Miles continues his introduction to Aspect-Oriented Programming (AOP) in Spring by delving into the around advice, which allows you to not just add to an existing method implementation, but to completely replace it.

方向:分布式系统设计

posted on 2005-04-08 15:15 java光环 阅读(6048) 评论(1)  编辑  收藏 所属分类: spring

java aop管理权限_基于spring aop 权限管理系统原型 - andyj2ee - BlogJava相关推荐

  1. java足球管理界面_基于jsp的足球俱乐部管理系统-JavaEE实现足球俱乐部管理系统 - java项目源码...

    基于jsp+servlet+pojo+mysql实现一个javaee/javaweb的足球俱乐部管理系统, 该项目可用各类java课程设计大作业中, 足球俱乐部管理系统的系统架构分为前后台两部分, 最 ...

  2. java车险管理软件_基于JSP车辆保险管理系统的开发与实现

    龙源期刊网 http://www.qikan.com.cn 基于 JSP 车辆保险管理系统的开发与实现 作者:陈玮楠 来源:<智富时代> 2019 年第 04 期 [摘 要]在当代,社会经 ...

  3. spring aop不执行_使用Spring AOP重试方法执行

    spring aop不执行 我的一位博客关注者发送了一封电子邮件,要求我显示" Spring AOP的RealWorld用法"示例. 他提到,在大多数示例中,都演示了Spring ...

  4. Java设计养老院系统_基于JavaWeb的养老院管理系统设计任务书

    主要参考文献(资料): [1] 熊杰. 宾馆住宿管理系统的设计[J]. 中国科技博览, 2015(29). [2] 孟小峰, 周龙骧, 王珊. 数据库技术发展趋势[J]. 软件学报, 2004, 15 ...

  5. python 工资管理软件_基于[Python]的员工管理系统

    基于[Python]的员工管理系统 -------------------------------- 简介 使用python语言来完成一个员工管理系统,员工信息包含:员工工号,姓名, 年龄,性别,职位 ...

  6. java快递员送货问题_基于javaMVC实现快递员管理系统

    [实例简介] 基于javaMVC实现快递员管理系统基于javaMVC实现快递员管理系统 [实例截图] [核心代码] 快递员 └── chahua3109 ├── assets │   ├── avat ...

  7. dubbo全局异常处理_基于spring aop的dubbo异常统一处理

    dubbo统一异常处理,调用方只显示封装后的异常. 1.返回封装后的Exception 2.返回封装后的统一返回信息 import org.aspectj.lang.annotation.AfterT ...

  8. 基于java的车辆维护系统设计_基于SSM车辆维修管理系统-JavaWeb汽车保养管理系统...

    需求分析 基于ssm框架实现一个车辆维修管理系统(java+springmvc+mybatis+mysql),能够实现用户信息管理.车辆信息管理.故障信息管理.维修订单管理.零件管理.统计管理等功能. ...

  9. 基于java web的图书馆_基于Java-Web的图书管理系统的设计与实现.doc

    题 目 基于Java Web的图书管理 系统的设计与实现 指导老师 ****** 专业班级 姓 名 ******* 学 号 ***** 年 月*日 第 PAGE \* MERGEFORMAT 0 页 ...

最新文章

  1. 2018目标,提高免疫力,身体工作双丰收
  2. leetcode 刷题 118. 杨辉三角解题思路
  3. java 配置hdfs集群_Hadoop集群搭建-04安装配置HDFS
  4. 如何在Smartphone模拟器上测试短信和电话
  5. wlnmp+nginx+mysql+php集合包_LNMP(Linux+Nginx+MySQL+PHP)部署详解(一)
  6. python千行代码项目_p2:千行代码入门python
  7. AVAssetWriter写入char*数据(video)
  8. Atitit.视频文件加密的方法大的总结 java c# php
  9. debian远程桌面设置
  10. 关于E-Prime 2.0 无法呈现音频的一种解决方案
  11. bat脚本变量赋值输出时提示”ECHO 处于关闭状态“
  12. Android——实现光点模糊渐变的自旋转圆环特效
  13. allergo 命令
  14. python 必应搜索教程
  15. Echarts图例位置 - legend属性
  16. 愿得一心人:硅谷亿万富豪们的婚姻怎样?有人白首相守七十年
  17. 最新版HBuilderx + 夜神模拟器 模拟器调试设置
  18. 如何抓包分析BLE 空口报文(GAP + GATT + LESC procedure)?
  19. linux进程命令面试,面试常问的 25+ 个 Linux 命令
  20. php验证电话号码是否合法,js代码验证手机号码和电话号码是否合法_javascript技巧...

热门文章

  1. Android开发,你应该知道的
  2. CreateThread用法详解
  3. 解决RichEdit line insertion error的方法(转载)
  4. java并发2--进阶
  5. JS让文本以打字效果呈现出来
  6. UIApplication深入研究
  7. AMIO编辑器开发(四):五一劳动节的编程较量,C++语言的设计模式
  8. windows--reg--向注册表中写入一些数据
  9. AndroidStudio_Android Studio项目中报Call requires API level 18 (current min is 16)---Android原生开发工作笔记232
  10. 大数据之-Hadoop完全分布式_完全分布式配置总结---大数据之hadoop工作笔记0040