#
cp
2024-08-13 12e16a4e48fa33ead5aae7ca065fdea636135527
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
package com.zy.asrs.openapi.config;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zy.asrs.common.openapi.service.ApiLogService;
import com.zy.asrs.common.utils.IpTools;
import com.zy.asrs.framework.common.R;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
 
@Component
@Aspect
@Slf4j
public class AspectConfig {
 
    @Autowired
    private ApiLogService apiLogService;
 
    @Pointcut("execution(* com.zy.asrs.openapi.controller..*.*(..))")
    private void webLog() {
    }
 
    @Around("@within(org.springframework.web.bind.annotation.RestController)" +
            "||@within(org.springframework.stereotype.Controller)")
    public Object after(ProceedingJoinPoint joinPoint) throws Throwable{
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        log.info("请求日志的打印");
        log.info("请求地址:{}", Optional.ofNullable(request.getRequestURI().toString()).orElse(null));
        log.info("请求方式:{}",request.getMethod());
        log.info("请求类方法:{}",joinPoint.getSignature());
        log.info("请求类方法参数:{}", JSONObject.toJSONString(filterArgs(joinPoint.getArgs())));
        long start = System.currentTimeMillis();
        Object result = joinPoint.proceed(joinPoint.getArgs());
        log.info("请求响应参数{}", JSONObject.toJSONString(result));
 
        // 保存接口日志
        Object appAuth = request.getAttribute("appAuth");
        String appkey = request.getHeader("appkey");
        apiLogService.saveLog(
                String.valueOf(appAuth),
                request.getRequestURI(),
                appkey,
                IpTools.gainRealIp(request),
                JSONObject.toJSONString(filterArgs(joinPoint.getArgs())),
                JSON.toJSONString(result),
                String.valueOf(((R) result).get("code")).equalsIgnoreCase("200")
        );
 
        long end = System.currentTimeMillis();
        log.info("执行耗时:{}", end - start);
        return result;
    }
 
    private List<Object> filterArgs(Object[] objects) {
        return Arrays.stream(objects).filter(obj -> !(obj instanceof MultipartFile)
                && !(obj instanceof HttpServletResponse)
                && !(obj instanceof HttpServletRequest)).collect(Collectors.toList());
    }
 
}