ITPub博客

首页 > IT职业 > IT职场 > 实战课程->电商后台->动态代理实现权限拦截

实战课程->电商后台->动态代理实现权限拦截

IT职场 作者:hoonty 时间:2021-09-09 09:06:26 0 删除 编辑
package com.mmall.proxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import com.mmall.common.ResponseCode;
import com.mmall.common.ServerResponse;
import com.mmall.pojo.User;
import com.mmall.service.IUserService;
public class AuthorityProxy implements InvocationHandler {
    private Object target;
    private Object obj;
    private IUserService iUserService;
    public AuthorityProxy(Object obj, IUserService iUserService) {
        this.obj = obj;
        this.iUserService = iUserService;
    }
    public Object bind(Object target) {
        this.target = target;
        return Proxy.newProxyInstance(target.getClass().getClassLoader(),
                target.getClass().getInterfaces(), this);
    }
    @Override
    public Object invoke(Object proxy, Method method, Object[] args)
            throws Throwable {
        if (obj instanceof User) {
            if (iUserService.checkAdminRole((User) obj).isSuccess()) {
                return method.invoke(target, args);
            }
            return ServerResponse.createByErrorMessage("无权限操作");
        }
        return ServerResponse.createByErrorCodeMessage(
                ResponseCode.NEED_LOGIN.getCode(), "用户未登录,请登录管理员");
    }
}

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4222/viewspace-2799496/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-12

  • 博文量
    94
  • 访问量
    1271