首先第一步我们需要定义一个接口UserDao
package cn.dhcool.proxy.auth;
public interface UserDao {
@Auth("admit")
public abstract void addUser();
@Auth("admit")
public abstract void delUser();
@Auth("admit")
public abstract User findUser();
@Auth("admit,user")
public abstract void updataUser();
}
一个实现类UserDaoImp
package cn.dhcool.proxy.auth;
public class UserDaoImp implements UserDao {
@Auth("admit")
public void addUser(){
System.out.println("add");
}
@Auth("admit")
public void delUser(){
System.out.println("delUser");
}
@Auth("admit")
public User findUser(){
System.out.println("findUser");
return null;
}
@Auth("admit,user")
public void updataUser(){
System.out.println("updataUser");
}
}
基本用户权限Domain: User
package cn.dhcool.proxy.auth;
public class User {
private String userName;
private String role;
public User() {
}
public User(String userName, String role) {
this.userName = userName;
this.role = role;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
}
用于获取动态代理类的工具类 : ProxyClass
package cn.dhcool.proxy.auth;
import java.lang.reflect.Proxy;
public class ProxyClass {
//真实对象
private Object realObject;
private User user;
public ProxyClass(Object realObject,User user) {
this.realObject = realObject;
this.user = user;
}
public Object newProxyInstance(){
return Proxy.newProxyInstance(this.realObject.getClass().getClassLoader(),
this.realObject.getClass().getInterfaces(),new MyInvocationHandler(realObject,user));
}
}
注解的定义:Auth
package cn.dhcool.proxy.auth;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Auth {
public String value();
}
接口回调:MyInvocationHandler
package cn.dhcool.proxy.auth;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
public class MyInvocationHandler implements InvocationHandler{
private Object realObject;
private User user;
public MyInvocationHandler(Object realObject,User user){
this.realObject = realObject;
this.user = user;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
String role = user.getRole();
//获取代理类调用的方法名
String methodName = method.getName();
//反射获取真实类的方法
Method realMethod = this.realObject.getClass().getMethod(methodName,method.getParameterTypes());
//判断真实的方法是否有注解
if(realMethod.isAnnotationPresent(Auth.class)){
Auth auth = realMethod.getAnnotation(Auth.class);
String[] auths = auth.value().split(",");
//循环遍历该方法能访问的角色是否包含该用户的角色
for (int i = 0; i < auths.length; i++) {
if(auths[i].equals(role)){
return realMethod.invoke(realObject, args);
}
}
throw new RuntimeException("你没有这个权限");
}
return null;
}
}
客户端调用:
package cn.dhcool.proxy.auth;
public class Client {
public static void main(String[] args) {
//创建真实对象
UserDao userDao = new UserDaoImp();
//创建用户
User user = new User("lhc","user");
ProxyClass proxyClass = new ProxyClass(userDao, user);
UserDao userDaoProxy = (UserDao)proxyClass.newProxyInstance();
userDaoProxy.updataUser();
}
}