郑永安
2023-06-19 7a6abd05683528032687c75e80e0bd2030a3e46c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package com.gkhy.safePlatform.account.common.aop;
 
import com.alibaba.fastjson.JSONObject;
import com.gkhy.safePlatform.account.model.annotation.CommonLogEnable;
import com.gkhy.safePlatform.account.model.constant.LogTemplate;
import com.gkhy.safePlatform.commons.co.ContextCacheUser;
import com.gkhy.safePlatform.commons.enums.Module;
import org.apache.commons.io.IOUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
 
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
 
@Aspect
@Component("accountControllerReqLogAspect")
public class CommonLogAspect {
 
    private static Logger log = LoggerFactory.getLogger(CommonLogAspect.class);
 
    @Around("execution(* com.gkhy.safePlatform.account.controller.*.*(..)) && @annotation(commonLogEnable)")
    public Object controllerReqParamLog(ProceedingJoinPoint point, CommonLogEnable commonLogEnable) throws Throwable {
 
        // 入参记录打印
        this.record(point);
        // 执行
        Object proceed = point.proceed();
        // todo 执行成功保存操作记录
        return proceed;
    }
 
    private void record(ProceedingJoinPoint point) {
        log.info(LogTemplate.REQUEST_RECORD_START);
        MethodSignature methodSignature = (MethodSignature) point.getSignature();
        Method method = methodSignature.getMethod();
        CommonLogEnable annotation = method.getAnnotation(CommonLogEnable.class);
        // 模块
        log.info(LogTemplate.REQUEST_MODULE, annotation.module().value);
        log.info(LogTemplate.REQUEST_INTRO, annotation.content());
 
        // 请求参数
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (requestAttributes != null) {
            HttpServletRequest request = requestAttributes.getRequest();
            log.info(LogTemplate.REQUEST_IP, getIp(request));
            if (request.getUserPrincipal() instanceof Authentication) {
                Authentication authentication = (Authentication) request.getUserPrincipal();
                ContextCacheUser requester = (ContextCacheUser) authentication.getPrincipal();
                log.info(LogTemplate.REQUEST_OWNER, requester.getRealName(), requester.getUid(), requester.getRoleCode());
            }
            log.info(LogTemplate.REQUEST_AGENT, request.getHeader("User-Agent"));
            log.info(LogTemplate.REQUEST_URL, request.getRequestURL());
            log.info(LogTemplate.REQUEST_PARAMS, JSONObject.toJSONString(request.getParameterMap()));
            // 支持 application/json 类型返回,不建议使用 request 读取 body ,getInputStream() 仅能使用一次
            String body = "";
            Annotation[][] annotations = method.getParameterAnnotations();
            for (int i = 0; i < annotations.length; i++) {
                for (Annotation b : annotations[i]) {
                    if (b.annotationType().equals(RequestBody.class)) {
                        body = JSONObject.toJSONString(point.getArgs()[i]);
                        break;
                    }
                }
            }
            log.info(LogTemplate.REQUEST_BODY, body);
        }
        String className = point.getTarget().getClass().getName();
        String methodName = methodSignature.getName();
        log.info(LogTemplate.REQUEST_LOCATION,className,methodName);
        log.info(LogTemplate.REQUEST_RECORD_END);
    }
 
 
    private String getIp(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
        }
}