切面中使用反射技术

package cn.com.demo.abby.common.aspect;

import cn.com.swancloud.aliyun.Authentication;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.profile.DefaultProfile;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.*;

/**
 * @Date: 2019/1/16 16:13
 * 生成认证对象切面
 */
@Aspect
@Component
public class AuthenticationAspect {

    private final String regionId = "regionId";
    private final String AccessKey_ID = "AccessKey_ID";
    private final String AccessKey_Secret = "AccessKey_Secret";

    /**
     * 对指定包,进行横切,创建一个横切的对象方法
     */
    @Pointcut("execution(* cn.com.demo.abby.service..*.*(..))")
    public void executionPoint() {}

    @Before("executionPoint()")
    public void before(JoinPoint joinPoint) {
        try {
            Object[] o = joinPoint.getArgs();
            for (int i = 0; i < o.length; i++) {

                //1.加载类
                String name = o[i].getClass().getName();
                Class clz = Class.forName(name);

                //2.获取当前类包含其父类的全部属性
                List<Field> fieldList = new ArrayList<>();
                while(clz != null){
                    fieldList.addAll(Arrays.asList(clz.getDeclaredFields()));
                    clz = clz.getSuperclass();
                }

                //3.匹配到iAcsClient属性,赋值
                for(Field field : fieldList){
                    if(("iAcsClient").equals(field.getName())){
                        //4.如果此属性为非public属性的情况时,需要设置属性可达,否则会抛出IllegalAccessException异常
                        field.setAccessible(true);
                        //5.生成认证对象
                        //5.1获取指定属性值
                        Map<String, String> fieldsValue = getFieldsValue(fieldList, o[i]);
                        //5.2创建 Profile。生成 IClientProfile 的对象 profile,该对象存放 AccessKeyID 和 AccessKeySecret 和默认的地域信息
                        DefaultProfile profile = DefaultProfile.getProfile(fieldsValue.get(regionId), fieldsValue.get(AccessKey_ID), fieldsValue.get(AccessKey_Secret));
                        //5.3创建 Client。从 IClientProfile 类中再生成 IAcsClient 的对象 client,后续获得 response 都需要从 IClientProfile 中获得。
                        IAcsClient client = new DefaultAcsClient(profile);
                        //6.赋值
                        field.set(o[i],client);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private Map<String,String> getFieldsValue(List<Field> fieldList,Object object) throws Exception {
        Map<String,String> result = new HashMap<>();
        for(Field field : fieldList){
            if(("regionId").equals(field.getName())){
                //如果此属性为非public属性的情况时,需要设置属性可达,否则会抛出IllegalAccessException异常
                field.setAccessible(true);
                //获取属性值存入Map
                result.put(regionId,field.get(object).toString());
            }
            if(("authentication").equals(field.getName())){
                field.setAccessible(true);
                //获取属性值存入Map
                Authentication authentication = (Authentication)field.get(object);
                result.put(AccessKey_ID,authentication.getAccessKey_ID());
                result.put(AccessKey_Secret,authentication.getAccessKey_Secret());
            }
        }
        return result;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值