在做权限控制的时候,需要有粗粒度和细粒度的权限控制,对于细粒度的程度,一般是对方法进行拦截,在调用方法的时候,对用户进行检查是否有权限,如果有权限则调用,否则不能调用。
1. 首先定义接口
package com.test.service;
// 业务接口
public interface IAction {
// 业务处理过程
public void doProcess();
}
2. 定义实现类
package com.test.service.impl;
import com.test.service.IAction;
public class ActionImpl implements IAction {
@Override
public void doProcess() {
System. out .println( " 业务处理 " );
}
}
3. 定义代理工厂( JDK 版本)
package com.test.aop;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import com.test.service.IAction;
public class ProxyFactory implements InvocationHandler {
private Object targetObject ;
public Object createProxyInstance(Object targetObject) {
this . targetObject = targetObject;
return Proxy.newProxyInstance (targetObject.getClass().getClassLoader(), targetObject.getClass().getInterfaces(), this );
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
IAction action = (IAction) targetObject ;
Object result = null ;
try {
System. out .println( " 检查权限 ......." );
result = method.invoke(action, args);
System. out .println( " 检查权限完毕 ...." );
} catch (Exception e) {
System. out .println( " 运行时发生异常 ..." );
} finally {
System. out .println( " 调用结束 ..." );
}
return result;
}
}
4. 测试类
package com.test.app;
import com.test.aop.ProxyFactory;
import com.test.service.IAction;
import com.test.service.impl.ActionImpl;
public class App {
public static void main(String[] args) {
ProxyFactory factory = new ProxyFactory();
IAction action = (IAction)factory.createProxyInstance( new ActionImpl());
action.doProcess ();
}
}
执行结果
检查权限 .......
业务处理
检查权限完毕 ....
调用结束 ...
以上采用 JDK 的反射机制和动态代理生成了代理对象 , 并进行方法拦截 , 但是对于没有实现接口的类不能采用以上方法产生代理类,此时可以用 cglib 的 jar 包,这里采用 cglib-nodep-2.1_3.jar ,在 spring 的项目中可以找到该 jar 包,将此 jar 包加载到工程里
采用 cglib 的方式如下:
package com.test.aop;
import java.lang.reflect.Method;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
public class CglibProxyFactory implements MethodInterceptor {
private Object targetObject ;
public Object createProxyInstance(Object targetObject) {
this . targetObject = targetObject;
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(targetObject.getClass());
enhancer.setCallback( this );
return enhancer.create();
}
@Override
public Object intercept(Object proxy, Method method, Object[] args,
MethodProxy methodproxy) throws Throwable {
Object result = null ;
try {
System. out .println( " 检查权限 ......." );
result = methodproxy.invoke( targetObject , args);
System. out .println( " 检查权限完毕 ...." );
} catch (Exception e) {
System. out .println( " 运行时发生异常 ..." );
} finally {
System. out .println( " 调用结束 ..." );
}
return result;
}
}
然后修改测试类
package com.test.app;
import com.test.aop.CglibProxyFactory;
import com.test.service.impl.ActionImpl;
public class App {
public static void main(String[] args) {
CglibProxyFactory factory = new CglibProxyFactory();
ActionImpl action = (ActionImpl)factory.createProxyInstance( new ActionImpl());
action.doProcess();
}
}
打印结果和前面一样
检查权限 .......
业务处理
检查权限完毕 ....
调用结束 ...