动态代理+注解实现用户权限控制

首先第一步我们需要定义一个接口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();
	}
}
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值