- 浏览: 499090 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (150)
- JavaScript (4)
- Jdbc (2)
- Jquery (5)
- Jndi (1)
- HttpClient (3)
- Jsoup (1)
- Linux (9)
- Lucene (5)
- Ubuntu (7)
- Mysql (16)
- Oracle (2)
- Mongodb (12)
- Tomcat (3)
- Hibernate (1)
- Spring (11)
- Struts2 (2)
- 开发工具 (4)
- Servlet (3)
- MVC (1)
- Json (4)
- 敏捷开发 (1)
- maven (6)
- 框架整合 (2)
- Java Core (8)
- Others (6)
- 网络服务 (2)
- Tags (1)
- Tools (12)
- 代码管理 (8)
- Redis (9)
- Log4j (1)
最新评论
-
ipodao:
果然有高人翻译啊,谢了~
dom4j api 详解 -
zhuwensheng:
字符串都不用单引号引着吗?
Oracle+110个常用函数 -
宋建勇:
...
HttpClient 处理中文乱码
此权限管理系统把待访问的业务层方法做为权限管理中的资源,通过spring aop 对接口方法进行拦截,来实现权限的管理,可以实现细粒度的权限控制。
在上文体验了spring aop 一些特性,aop 接口:MethodBeforeAdvice, AfterReturningAdvice, ThrowsAdvice 实现这三个接口分别对方法执行前,后,执行中抛异常等情况进行的,我们要是想做overload 这样的操作时,要用MethodInterceptor 接口,此接口好在有返回值,
public Object invoke(
MethodInvocation invocation)
throws Throwable
{
//.
}
上文做法有些牵强业务逻辑还有throws PermissionDeniedException 感觉不爽,现在用MethodInterceptor 接口,来写这个demo,把权限与业务分开。
advice 如下:
public class PermissionCheckAroundAdvice implements MethodInterceptor {
SecurityManager securityMgr = new SecurityManager();
/**//**
* @param securityMgr The securityMgr to set.
*/
public void setSecurityMgr(SecurityManager securityMgr) {
this.securityMgr = securityMgr;
}
public Object 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))
return invocation.proceed();
System.out.println("Goodbye! NO Permission!(by " + this.getClass().getName() + ")");
return "--";
}
}
服务层业务接口修改如下:
public interface Service {
public String getBeanInfo();
}
服务层业务实现类如下:
public class ServiceBean implements Service {
ResourceBean bean;
/**//**
* @param bean The bean to set.
*/
public void setBean(ResourceBean bean) {
this.bean = bean;
}
public String getBeanInfo(){
String result="";
result+= bean.getMethod1();
result+= bean.getMethod2();
result+= bean.getMethod3();
return result;
}
}
资源层,接口 ,类如下:
public interface Resource {
}
public interface ResourceBean extends Resource{
public void theMethod();
public String getMethod1();
public String getMethod2();
public String getMethod3();
}
public class ResourceBeanImpl implements ResourceBean,InitializingBean{
public void theMethod(){
System.out.println(this.getClass().getName()
+ "." + new Exception().getStackTrace()[0].getMethodName()
+ "()"
+ " says HELLO!");
}
public String getMethod1(){
return "张三";
}
public String getMethod2(){
return "李四";
}
public String getMethod3(){
return "王五";
}
public void afterPropertiesSet() throws Exception {
System.out.println("事件监听:类ResourceBeanImpl属性设置完毕");
}
}
权限管理类:
public class User {
List privilages = new java.util.ArrayList();
String name;
public User(){
}
/**//**
* @param privilages The privilages to set.
*/
public void setPrivilages(List privilages) {
this.privilages = privilages;
}
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public boolean 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;
}
}
return pass;
}
}
public class SecurityManager {
User user;
public void setUser(User user){
this.user = user;
}
public boolean checkPermission(String privilege){
return checkPermission(user,privilege);
}
public boolean checkPermission(User user, String privilege){
return user.isPermission(privilege);
}
}
配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!--CONFIG-->
<bean id="bean" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>com.jhalo.jsecurity.aop.ResourceBean</value>
</property>
<property name="target">
<ref local="beanTarget"/>
</property>
<property name="interceptorNames">
<list>
<value>permissionAroundAdvisor</value>
</list>
</property>
</bean>
<bean id="service" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>com.jhalo.jsecurity.aop.Service</value>
</property>
<property name="target">
<ref local="serviceBean"/>
</property>
<property name="interceptorNames">
<list>
<value>permissionAroundAdvisor</value>
</list>
</property>
</bean>
<!--CLASS-->
<bean id="resourceMgr" class="com.jhalo.jsecurity.aop.ResourceManager"/>
<bean id="beanTarget" class="com.jhalo.jsecurity.aop.ResourceBeanImpl"/>
<bean id="beanTarget2" class="com.jhalo.jsecurity.aop.ResourceBean2Impl"/>
<bean id="user" class="com.jhalo.jsecurity.aop.User">
<property name="name">
<value>tester</value>
</property>
<property name="privilages">
<list>
<value>com.jhalo.jsecurity.aop.ResourceBean.getMethod3</value>
<value>com.jhalo.jsecurity.aop.Service.getBeanInfo</value>
<value>com.jhalo.jsecurity.aop.ResourceBean.getMethod1</value>
</list>
</property>
</bean>
<bean id="securityMgr" class="com.jhalo.jsecurity.aop.SecurityManager">
<property name="user">
<ref local="user"/>
</property>
</bean>
<bean id="serviceBean" class="com.jhalo.jsecurity.aop.ServiceBean">
<property name="bean">
<!-- <ref local="beanTarget"/>-->
<ref local="bean"/>
</property>
</bean>
<!--ADVISOR-->
<!--Note: An advisor assembles pointcut and advice-->
<!-- -->
<!-- permission around advisor -->
<bean id="permissionAroundAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice">
<ref local="thePermissionAroundAdvice"/>
</property>
<property name="pattern">
<value>.*</value>
</property>
</bean>
<!--ADVICE-->
<bean id="thePermissionCheckBeforeAdvice" class="com.jhalo.jsecurity.aop.PermissionCheckAdvice"/>
<bean id="thePermissionThrowsAdvice" class="com.jhalo.jsecurity.aop.PermissionThrowsAdvice"/>
<bean id="thePermissionAroundAdvice" class="com.jhalo.jsecurity.aop.PermissionCheckAroundAdvice">
<property name="securityMgr">
<ref local="securityMgr"/>
</property>
</bean>
</beans>
User 所拥有的权限是在spring 配置文件中手工配置的,在实际应用中不可行,可以从DB中取得。
测试类:
public class SpringAopTest {
public static void main(String[] args) {
//Read the configuration file
ApplicationContext ctx
= new FileSystemXmlApplicationContext("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);
}
}
发表评论
-
spring3 使用注解配置quartz定时任务
2013-03-13 16:09 2043spring3 使用注解配置quartz定时任务 ... -
spring 使用xml配置文件实现quartz作业调度
2013-03-13 16:06 2130spring 使用xml配置文件实现quartz作业调度 ... -
从springMVC项目中导出数据到Excel表格
2012-06-26 11:03 9718核心思路: 1.视图: ... -
批量文件上传,压缩后保存到服务器
2012-06-19 23:20 2185/** * 按照自己的规则重命名上传文件 ... -
Spring3.1 文件上传
2012-06-19 23:06 9648注意:以下上传和下载方法未必完全正确,不同浏览器效果不同,建议 ... -
快速搭建SSH三大框架(spring2.5)
2012-06-18 23:02 37821.创建web项目,名称为jboa 2.创建java ... -
注解配置 javabean hibernate 关联关系
2012-06-15 18:16 1791import javax.persistence.*; ... -
spring 中使用jquery实现ajax异步调用
2012-06-15 18:12 3014功能:创建班级前首先检查班级是否存在 1.Con ... -
springmvc 使用json传递数据
2012-06-15 18:11 2994需要使用的框架 spring3.0 jquery(简 ... -
Spring3.X 注解精华
2012-06-02 00:49 26640.提示 1) Spring发行版本附带了PetCl ...
相关推荐
基于spring_aop_权限管理系统原型
依赖注入来管理各层,面向切面编程管理事务,日志和权限 MyBatis:持久层;访问数据库;基于jdbc的框架,主要用来操作数据库,并且将业务实体和数据表联系起来 1、Spring (1)基本概念 Spring是一个开源开发框架...
依赖注入来管理各层,面向切面编程管理事务,日志和权限 MyBatis:持久层;访问数据库;基于jdbc的框架,主要用来操作数据库,并且将业务实体和数据表联系起来 1、Spring (1)基本概念 Spring是一个开源开发框架...
Spring AOP 模块为基于 Spring 的应用程序中的对象提供了事务管理服务。通过使用 Spring AOP,不用依赖 EJB 组件,就可以将声明性事务管理集成到应用程序中。 ☆ Spring DAO:JDBC DAO 抽象层提供了有意义的异常...
1、基于JavaWEB+SSM+mysql框架构建的在线商城系统源码+数据库+项目说明(课程设计).zip 2、该资源包括项目的全部源码,下载可以直接使用! 3、本项目适合作为计算机、数学、电子信息等专业的课程设计、期末大作业和...
Coalesys PanelBar + R.a.d Treeview +Xml 构建的Asp.net 菜单和权限管理模块 突破屏蔽限制,自己的网站使劲弹新IE窗口 对页面SCROLLING的CSS不能生效原因 .Net 中IE使用WinForm控件的使用心得。 动态加载用户控件的...
软件系统设计⽅案 软件系统设计⽅案 前⾔ 前⾔ 本⽂根据⾼级软件⼯程课上所学知识,对⼯程实践项⽬-⽹上书城进⾏软件系统分析和设计,最终形成软件系统概念原型。 参考资料: ⼀、系统架构 ⼀、系统架构 系统采⽤MVC...