zhuguifei
2026-03-10 58402bd5e762361363a0f7d7907153c77dbb819f
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
package com.shlanbao.tzsc.utils.tools;
 
import java.lang.reflect.Method;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.*;
 
import com.shlanbao.tzsc.base.model.SessionInfo;
import com.shlanbao.tzsc.pms.sys.log.beans.LogBean;
import com.shlanbao.tzsc.pms.sys.log.service.LogServiceI;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import javax.servlet.http.HttpServletRequest;
 
/**
 * @author: sunzhen
 * @date: 2019/7/31
 * @time: 18:18
 * @description: AOP实现日志
 */
@Component
@Aspect
public class LogAopAspect {
 
    @Autowired
    private LogServiceI logServiceI;// 日志Service
    @Autowired
    private HttpServletRequest request;
    /**
     * 环绕通知记录日志通过注解匹配到需要增加日志功能的方法
     * 
     * @param pjp
     * @return
     * @throws Throwable
     */
    @Around("@annotation(com.shlanbao.tzsc.utils.tools.LogAnno)")
    public Object aroundAdvice(ProceedingJoinPoint pjp) throws Throwable {
        // 1.方法执行前的处理,相当于前置通知
        // 获取方法签名
        MethodSignature methodSignature = (MethodSignature) pjp.getSignature();
        // 获取方法
        Method method = methodSignature.getMethod();
        /*// 获取方法参数名称数组
        String[] parameterNames = methodSignature.getParameterNames();
        // 获取方法参数值数组
        Object[] args = pjp.getArgs();*/
        // 获取方法上面的注解
        LogAnno logAnno = method.getAnnotation(LogAnno.class);
        // 获取操作描述的属性值
        String operateType = logAnno.operateType();
        // 创建一个日志对象(准备记录日志)
        LogBean logBean=new LogBean();
        logBean.setOptname(operateType);// 操作说明
 
        // 从session中获取登录者信息
        SessionInfo sessionInfo = (SessionInfo) request.getSession().getAttribute("sessionInfo");//获取session中的user对象进而获取操作人名字
        logBean.setName(sessionInfo.getUser().getName());// 设置操作人
        // 获取IP
        logBean.setIp(sessionInfo.getIp());
        // 获取详情
        Map map = new HashMap();
        Enumeration paramNames = request.getParameterNames();
        while (paramNames.hasMoreElements()) {
            String paramName = (String) paramNames.nextElement();
            String[] paramValues = request.getParameterValues(paramName);
            if (paramValues.length == 1) {
                String paramValue = paramValues[0];
                if(paramValue.length()>500){
                    paramValue  = cutStr(paramValue, 500);
                }
                if (paramValue.length() != 0) {
                    map.put(paramName, paramValue);
                }
            }
        }
        logBean.setParams("方法名:"+method.getName()+" 参数列表:"+map.toString());
 
        // 配置项目名
        logBean.setSys("PMS");
        // 配置DEL(默认为0)
        logBean.setDel(0L);
 
        /*// 获取方法参数名称数组
        String[] parameterNames = methodSignature.getParameterNames();
        Object[] args = pjp.getArgs();
        int index = ArrayUtil.indexOf(parameterNames, "id");
        if(index!=-1){
            // 获取详情
            logBean.setParams("方法名:"+method.getName()+" 参数id:"+ args[index]);
        }else{
            // 获取详情
            logBean.setParams("方法名:"+method.getName()+" 参数名:"+ Arrays.toString(parameterNames));
        }*/
 
        Object result = null;
        try {
            //让代理方法执行
            result = pjp.proceed();
            // 2.相当于后置通知(方法成功执行之后走这里)
            logBean.setSuccess("正常");// 设置操作结果
        } catch (Exception e) {
            // 3.相当于异常通知部分
            logBean.setSuccess("失败");// 设置操作结果
            throw e;
        } finally {
            // 4.相当于最终通知
            /*String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
            logBean.setDate(format);// 设置操作日期*/
            logServiceI.saveLog(logBean);// 添加日志记录
        }
        return result;
    }
 
    /**
     * 返回截取指定长度字节数后的字符串,多余部分用“...”代替
     */
    public static String cutStr(String strs, int length) {
        int sum = 0;
        String finalStr = "";
        if (null == strs || strs.getBytes().length <= length) {
            finalStr = (strs==null?"":strs);
        } else {
            for (int i = 0; i < strs.length(); i++) {
                String str = strs.substring(i, i + 1);
                // 累加单个字符字节数
                sum += str.getBytes().length;
                if (sum > length) {
                    finalStr = strs.substring(0, i) + "...";
                    break;
                }
            }
        }
        return finalStr;
    }
 
    /**
     * 获取IP地址的方法
     * @param request   传一个request对象下来
     * @return
     */
    public static String getIpAddress(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.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
}