继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

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

car
关注TA
已关注
手记 83
粉丝 56
获赞 363
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(), "用户未登录,请登录管理员");
    }
}
打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP