whycxzp
2025-03-18 be654227077149fe3fca4412bd3edd32afa43d47
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package com.whyc.aop;
 
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.whyc.constant.OperationLogEnum;
import com.whyc.dto.Response;
import com.whyc.service.OperationLogService;
import com.whyc.util.JsonUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.util.HashMap;
 
/**
 * 定义aop,对特定的接口进行内容捕捉,生成操作日志
 * 切面为controller
 * 切点为Point表达式
 * 具体连接点为JoinPoint
 * 处理方式为doAfterReturning
 */
 
@Component
@Aspect
public class OperationLogAspect {
 
    @Autowired
    private OperationLogService service;
 
    /**定义切点*/
    @Pointcut(value = "execution(public * com.whyc..controller.*.add*(..))" +
            "|| execution(public * com.whyc..controller.*.delete*(..))" +
            "|| execution(public * com.whyc..controller.*.update*(..))" +
            "|| execution(public * com.whyc..controller.LoginController.*(..))" +
            "|| execution(public * com.whyc..controller.UserInfController.resetSnId*(..))"
    )
    private void operationLogPointcut(){};
 
    /**aop处理类,对捕捉到的切点接口,进行数据处理*/
    @AfterReturning(pointcut = "operationLogPointcut()",returning = "resp")
    public void doAfterReturnOperation(JoinPoint point,Object resp){
        Response response = (Response) resp;
        Signature signature = point.getSignature();
        String methodSignature = signature.toString();
        //方法名
        // e.g:update
        String methodName = signature.getName();
        //执行的类全名
        // e.g: com.whyc.controller.CKPowerDevRtSetController
        String fullClassName = signature.getDeclaringTypeName();
        //获取类型
        String[] fullClassNameSplit = fullClassName.split("\\.");
        //类名
        // e.g: CKPowerDevRtSetController
        String classNameTrue = fullClassNameSplit[fullClassNameSplit.length - 1];
        Integer type1 = 0;
        Integer type2 = 0;
        String operationTypeName = "";
 
        //系统级
        if (classNameTrue.equals("UserInfController")) {
            if(methodName.contains("add")){ //用户新增
                type1 = OperationLogEnum.TYPE_1_SYS.getType();
                type2 = OperationLogEnum.TYPE_2_USER_ADD.getType();
                operationTypeName = OperationLogEnum.TYPE_2_USER_ADD.getName();
 
            }else if(methodName.contains("update")){ //用户修改
                type1 = OperationLogEnum.TYPE_1_SYS.getType();
                type2 = OperationLogEnum.TYPE_2_USER_UPDATE.getType();
                operationTypeName = OperationLogEnum.TYPE_2_USER_UPDATE.getName();
            }else if(methodName.contains("delete")){ //用户删除
                type1 = OperationLogEnum.TYPE_1_SYS.getType();
                type2 = OperationLogEnum.TYPE_2_USER_DELETE.getType();
                operationTypeName = OperationLogEnum.TYPE_2_USER_DELETE.getName();
            }else if(methodName.contains("resetSnId")){ //密码重置
                type1 = OperationLogEnum.TYPE_1_SYS.getType();
                type2 = OperationLogEnum.TYPE_2_USER_UPDATE_PASSWORD.getType();
                operationTypeName = OperationLogEnum.TYPE_2_USER_UPDATE_PASSWORD.getName();
            }
 
        }else if(classNameTrue.equals("LoginController")){
            if(methodName.equals("login")){
                if((boolean) response.getData()){ //用户登录
                    type1 = OperationLogEnum.TYPE_1_SYS.getType();
                    type2 = OperationLogEnum.TYPE_2_LOGIN.getType();
                    operationTypeName = OperationLogEnum.TYPE_2_LOGIN.getName();
                }else { //用户登录失败
                    type1 = OperationLogEnum.TYPE_1_SYS.getType();
                    type2 = OperationLogEnum.TYPE_2_LOGIN_FAIL.getType();
                    operationTypeName = OperationLogEnum.TYPE_2_LOGIN_FAIL.getName();
                }
            }
            else if(methodName.equals("logout")){ //用户退出
                type1 = OperationLogEnum.TYPE_1_SYS.getType();
                type2 = OperationLogEnum.TYPE_2_LOGOUT.getType();
                operationTypeName = OperationLogEnum.TYPE_2_LOGOUT.getName();
            }
            else if(methodName.equals("changeSnId")){ //用户密码重置
                type1 = OperationLogEnum.TYPE_1_SYS.getType();
                type2 = OperationLogEnum.TYPE_2_USER_UPDATE_PASSWORD.getType();
                operationTypeName = OperationLogEnum.TYPE_2_USER_UPDATE_PASSWORD.getName();
            }
        }else if(classNameTrue.contains("Alarm")
                ||classNameTrue.contains("Alm")){ //告警设置
            if(methodName.startsWith("update")){
                type1 = OperationLogEnum.TYPE_1_SERVICE.getType();
                type2 = OperationLogEnum.TYPE_2_ALARM.getType();
                operationTypeName = OperationLogEnum.TYPE_2_ALARM.getName();
            }
        }else if(classNameTrue.contains("plan")){
            if(methodName.startsWith("static")){//统计计划
                type1 = OperationLogEnum.TYPE_1_SERVICE.getType();
                type2 = OperationLogEnum.TYPE_2_PLAN_MON.getType();
                operationTypeName = OperationLogEnum.TYPE_2_PLAN_MON.getName();
            }
        }
 
        String module = "模块";
        String className = classNameTrue.replace("Controller", module);
 
       /* //操作时间
        Date operationTime = new Date();
        //客户端ip
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
        String terminalIp = request.getRemoteAddr();*/
 
        //操作信息
        //获取切点方法信息
        String[] parameterNames = ((MethodSignature) signature).getParameterNames();
        Object[] args = point.getArgs();
 
        HashMap<String, Object> map = new HashMap<>();
        for (int i = 0; i < parameterNames.length; i++) {
            if (args[i] != null) {
                StringBuilder paramBuilder = new StringBuilder();
                //String param = args[i].toString();
                try {
                    JsonObject paramJsonObject = JsonUtil.getGson().toJsonTree(args[i]).getAsJsonObject();
                    if(paramJsonObject.has("logList")){
                        paramJsonObject.remove("logList");
                        JsonElement jsonElement = new JsonPrimitive("logList字段忽略存储");
                        paramJsonObject.add("logList",jsonElement);
                    }
                    paramBuilder.append(paramJsonObject.toString());
                }catch (Exception e){
                    paramBuilder.append(args[i].toString());
                }
 
                /*String[] paramSetArr = param.split(",");
                int paramSetLength = paramSetArr.length;
                for (int j = 0; j < paramSetLength; j++) {
                    String paramSet = paramSetArr[j];
                    if(j == 0){ //首行,特殊情况,存在null但是要保留
                        if(paramSet.endsWith("=null")){
                            if(paramSet.contains("(")){
                                paramBuilder.append(paramSet.substring(0,paramSet.lastIndexOf("(")+1));
                                continue;
                            }
                        }
                    }else if(j == paramSetLength -1){ //末行,特殊情况,存在null但是要保留
                        if(paramSet.endsWith("=null)")){
                            paramBuilder.append(")");
                            continue;
                        }
                    }
                    //其他非null情况
                    if(!paramSet.endsWith("=null")){
                        paramBuilder.append(paramSet);
                    }
 
                }*/
                //map.put(parameterNames[i], args[i].toString());
                map.put(parameterNames[i], paramBuilder.toString());
            }
        }
        //登录接口的密码,不存储
        if (methodSignature.contains("LoginController.login")) {
            map.put("usnId","密码密文保密,忽略存储");
        }
        //修改密码接口的密码,也不存储
        else if(methodSignature.contains("LoginController.changeSnId")){
            map.put("oldSnId","旧密码密文保密,忽略存储");
            map.put("newSnId","新密码密文保密,忽略存储");
        }
        String operationMsg = "执行了" + className + "的" + operationTypeName + "操作.";
        String operationDetail = "具体调用方法为:" + methodName + ",具体参数为:" + map.toString();
 
        service.record(type1, type2, operationMsg, operationDetail);
 
    }
 
}