WebAspect.java 5.02 KB
Newer Older
yanzg's avatar
yanzg committed
1 2
package com.yanzuoguang.cloud.aop;

yanzg's avatar
yanzg committed
3
import com.yanzuoguang.cloud.service.TokenServiceCall;
yanzg's avatar
yanzg committed
4
import com.yanzuoguang.token.TokenHelper;
5
import com.yanzuoguang.util.base.ObjectHelper;
yanzg's avatar
yanzg committed
6
import com.yanzuoguang.util.exception.ExceptionHelper;
7
import com.yanzuoguang.util.helper.JsonHelper;
8
import com.yanzuoguang.util.helper.TypeHelper;
yanzg's avatar
yanzg committed
9
import com.yanzuoguang.util.log.Log;
yanzg's avatar
yanzg committed
10 11 12 13 14 15 16
import com.yanzuoguang.util.vo.ResponseResult;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
yanzg's avatar
yanzg committed
17
import org.springframework.beans.factory.annotation.Autowired;
yanzg's avatar
yanzg committed
18
import org.springframework.beans.factory.annotation.Value;
yanzg's avatar
yanzg committed
19
import org.springframework.context.ApplicationContext;
yanzg's avatar
yanzg committed
20 21 22
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
23
import java.lang.reflect.ParameterizedType;
yanzg's avatar
yanzg committed
24 25 26 27 28 29 30 31
import java.lang.reflect.Type;

/**
 * LogsAspect(接口请求日志切面)
 *
 * @author: Kang
 * @time: 2018年04月25日 11:43
 */
yanzg's avatar
yanzg committed
32 33
@Aspect
@Component
yanzg's avatar
yanzg committed
34
public class WebAspect extends BaseRequestAspect {
yanzg's avatar
yanzg committed
35

yanzg's avatar
yanzg committed
36 37
    private static final String TAG = WebAspect.class.getSimpleName();

yanzg's avatar
yanzg committed
38
    @Autowired
yanzg's avatar
yanzg committed
39
    private TokenServiceCall tokenServiceCall;
yanzg's avatar
yanzg committed
40

yanzg's avatar
yanzg committed
41 42 43
    @Value("${yzg.logWeb:false}")
    private boolean logWeb;

yanzg's avatar
yanzg committed
44 45 46
    @Value("${yzg.gateway:^.*gateway.*$}")
    private String gateWay;

yanzg's avatar
yanzg committed
47 48 49 50 51 52 53 54
    @Autowired
    private ApplicationContext context;

    /**
     * 是否存在初始化方法
     */
    private WebAssecptInit webAssecptInit;

yanzg's avatar
yanzg committed
55 56 57
    /**
     * exec aop point aspect
     */
yanzg's avatar
yanzg committed
58
    @Pointcut("execution(* *..web..*Controller.*(..))")
yanzg's avatar
yanzg committed
59 60 61
    public void webAspect() {
    }

yanzg's avatar
yanzg committed
62

yanzg's avatar
yanzg committed
63 64 65 66 67 68 69 70
    /**
     * 执行环形切面
     *
     * @param joinPoint
     * @return
     */
    @Around(value = "webAspect()")
    public Object requestWebAround(ProceedingJoinPoint joinPoint) throws Throwable {
yanzg's avatar
yanzg committed
71 72 73 74 75 76
        Object result = null;
        boolean isGateWay = isGateWay();
        if (isGateWay) {
            result = executeMethod(joinPoint);
            return result;
        }
yanzg's avatar
yanzg committed
77
        boolean clear = requestLogInit();
yanzg's avatar
yanzg committed
78
        // 用户数据库记录
yanzg's avatar
yanzg committed
79
        long start = requestLog(TAG, joinPoint);
yanzg's avatar
yanzg committed
80
        Exception ex = null;
yanzg's avatar
yanzg committed
81
        boolean isInit = false;
yanzg's avatar
yanzg committed
82
        try {
yanzg's avatar
yanzg committed
83
            isInit = tokenServiceCall.tokenInit();
yanzg's avatar
yanzg committed
84 85 86 87 88 89 90 91 92 93 94 95
            if (webAssecptInit == null) {
                try {
                    webAssecptInit = context.getBean(WebAssecptInit.class);
                } catch (Exception e) {
                    webAssecptInit = new WebAssecptInit() {
                        @Override
                        public void init(Object req) {
                        }
                    };
                    Log.error(WebAspect.class, "请设置登录默认处理函数");
                    Log.error(WebAspect.class, e);
                }
yanzg's avatar
yanzg committed
96 97 98 99 100 101
            }
            if (webAssecptInit != null) {
                for (Object arg : joinPoint.getArgs()) {
                    webAssecptInit.init(arg);
                }
            }
yanzg's avatar
yanzg committed
102
            result = executeMethod(joinPoint);
yanzg's avatar
yanzg committed
103 104
            return result;
        } catch (Exception e) {
yanzg's avatar
yanzg committed
105
            ex = e;
yanzg's avatar
yanzg committed
106
            result = ExceptionHelper.getError(e);
yanzg's avatar
yanzg committed
107
            Type returnType = getReturnType(joinPoint);
108 109
            Class returnClass = TypeHelper.getClass(returnType);
            if (TypeHelper.isClass(returnClass, ResponseResult.class)) {
yanzg's avatar
yanzg committed
110
                return result;
111 112
            } else if (TypeHelper.isSubType(returnType, ResponseResult.class)) {
                return JsonHelper.to(result, returnClass);
yanzg's avatar
yanzg committed
113 114 115 116
            } else {
                throw e;
            }
        } finally {
yanzg's avatar
yanzg committed
117
            tokenServiceCall.tokenFinish();
yanzg's avatar
yanzg committed
118
            if (isInit) {
yanzg's avatar
yanzg committed
119
                TokenHelper.remove();
yanzg's avatar
yanzg committed
120
            }
yanzg's avatar
yanzg committed
121
            responseLog(logWeb, clear, TAG, HttpAspectUtil.getHttpRequestUrl(), joinPoint, start, result, ex);
yanzg's avatar
yanzg committed
122 123 124
        }
    }

yanzg's avatar
yanzg committed
125 126 127 128 129 130 131 132 133
    /**
     * 是否属于网关服务,网关服务不进行监控
     *
     * @return
     */
    private boolean isGateWay() {
        return this.applicationName.matches(this.gateWay);
    }

yanzg's avatar
yanzg committed
134

yanzg's avatar
yanzg committed
135 136 137 138 139 140 141 142
    /**
     * 获取返回的至类型
     *
     * @param joinPoint
     * @return
     * @throws NoSuchMethodException
     */
    private Type getReturnType(ProceedingJoinPoint joinPoint) {
yanzg's avatar
yanzg committed
143 144 145 146 147 148 149 150 151 152 153 154 155
        Method m = getMethod(joinPoint);
        Type t = m.getAnnotatedReturnType().getType();
        return t;
    }

    /**
     * 获取返回的至类型
     *
     * @param joinPoint
     * @return
     * @throws NoSuchMethodException
     */
    private Method getMethod(ProceedingJoinPoint joinPoint) {
yanzg's avatar
yanzg committed
156 157 158 159
        //获取返回值类型
        Signature s = joinPoint.getSignature();
        MethodSignature ms = (MethodSignature) s;
        Method m = ms.getMethod();
yanzg's avatar
yanzg committed
160
        return m;
yanzg's avatar
yanzg committed
161 162
    }

yanzg's avatar
yanzg committed
163 164 165 166 167 168 169
    /**
     * 执行方法
     *
     * @param joinPoint 需要执行的方法
     * @return 返回结果
     * @throws Throwable
     */
yanzg's avatar
yanzg committed
170
    private Object executeMethod(ProceedingJoinPoint joinPoint) throws Throwable {
yanzg's avatar
yanzg committed
171
        return joinPoint.proceed();
yanzg's avatar
yanzg committed
172 173
    }
}