liurunyu
2024-04-24 e430bfca38cdae9dd230f951e108a75caebccb61
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package com.dy.pmsGlobal.aop;
 
import com.dy.common.util.ObjectToMapUtil;
import com.dy.common.webFilter.UserTokenContext;
import com.dy.common.webUtil.BaseResponse;
import com.dy.common.webUtil.BaseResponseUtils;
import com.dy.pmsGlobal.pojoBa.BaUser;
import com.mysql.cj.util.StringUtils;
import jakarta.servlet.http.HttpServletRequest;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.util.UriComponentsBuilder;
 
import java.util.Map;
 
@Aspect
@Component
public class LogAspect {
 
    @Value("${pms.sso.curUserUrl}")
    public String SsoCurUserUrl ;
 
    private LogSv logSv;
    @Autowired
    public void setLogSv(LogSv logSv){
        this.logSv = logSv;
    }
    private RestTemplate restTemplate;
    @Autowired
    public void setRestTemplate(RestTemplate restTemplate){
        this.restTemplate = restTemplate ;
    }
 
    @AfterReturning(pointcut = "@annotation(com.dy.pmsGlobal.aop.Log)", returning = "result")
    public void logAfterReturning(JoinPoint joinPoint, Object result) {
        // 获取方法的中文描述
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        Log operationDescription = methodSignature.getMethod().getAnnotation(Log.class);
        String operationName = operationDescription.value();
        //结果
        BaseResponse response = (BaseResponse)result;
        response.isSuccess();
        // 获取用户信息
        BaUser user = (BaUser)getCurUser(response);
        Long operator = user!=null?user.id: 1l;
 
        // 获取IP地址
        String ipAddress = getRemoteHost();
        // 记录日志
        logSv.save(operator, operationName,ipAddress);
    }
 
    /**
     * 调用SSO获取用户信息
     * @param response (获取token)
     * @return 返回对象
     */
    private Object getCurUser(BaseResponse response){
        if(!StringUtils.isNullOrEmpty(SsoCurUserUrl)){
            String token = UserTokenContext.get();
            if(StringUtils.isNullOrEmpty(token)){
                Map<String,Object> resMap = ObjectToMapUtil.objectToMap(response.getContent());
                token =resMap.containsKey("token")?resMap.get("token").toString():"";
            }
 
            String url = UriComponentsBuilder.fromUriString(SsoCurUserUrl)
                    .queryParam("token", token)
                    .build()
                    .toUriString();
            HttpHeaders headers = new HttpHeaders();
            HttpEntity<?> httpEntity = new HttpEntity<>(headers);
            ResponseEntity<BaUser> myResponse = null;
            try {
                // 通过Get方式调用接口
                myResponse = restTemplate.exchange(url, HttpMethod.GET, httpEntity, BaUser.class);
            } catch (Exception e) {
                e.printStackTrace();
            }
            assert myResponse != null;
            return myResponse.getBody();
        }else {
            return BaseResponseUtils.buildError("后端系统出错,未得到SsoCurUserUrl");
        }
    }
 
    /**
     * 获取IP地址
     * @return
     */
    private String getRemoteHost() {
        // 获取请求对象
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        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 "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : ip;
    }
}