1
hao
2025-05-20 8e24c6fea30d9b179375ee2893710cdec2443b13
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
package com.app.aspect;
 
import com.alibaba.fastjson.JSON;
import com.auth0.jwt.JWT;
import com.system.log.entity.SysLog;
import com.system.log.service.SysLogService;
import com.system.user.entity.SysUser;
 
import org.aspectj.lang.JoinPoint;
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 org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
 
import javax.servlet.http.HttpServletRequest;
 
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
import java.util.Date;
 
/**
 * 系统日志:切面处理类
 */
@Aspect
@Component
public class SysLogAspect {
 
    @Autowired
    private SysLogService sysLogService;
 
    //定义切点 @Pointcut
    //在注解的位置切入代码
   // @Pointcut("@annotation( com.app.aspect.MyLog)")
    @Pointcut("execution(public * com.*.*.controller..*.*(..))")
    public void logPoinCut() {
    }
 
    //切面 配置通知
    @AfterReturning("logPoinCut()")
    public void saveSysLog(JoinPoint joinPoint) {
        System.out.println("访问日志。。。。。。" + new Date());
        //保存日志
        SysLog sysLog = new SysLog();
 
        //从切面织入点处通过反射机制获取织入点处的方法
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        //获取切入点所在的方法
        Method method = signature.getMethod();
 
        //获取操作
      /*  MyLog myLog = method.getAnnotation(MyLog.class);
        if (myLog != null) {
            String value = myLog.value();
            sysLog.setOperation(value);//保存获取的操作
        }*/
 
        //获取请求的类名
        String className = joinPoint.getTarget().getClass().getName();
        //获取请求的方法名
        String methodName = method.getName();
        sysLog.setMethod(className + "." + methodName);
 
        //请求的参数
        Object[] args = joinPoint.getArgs();
        //将参数所在的数组转换成json
        String params = JSON.toJSONString(args);
        
        //System.out.println("String length: " + params.length());
        //System.out.println("Byte array length: " + params.getBytes().length);
        
        //20181225-fyx-参数过大(一般是文件)
        if(params.length()>=255){
            params = params.substring(0, 254);
        }
        System.out.println("String: " + params);
     
        try {
            params = new String(params.getBytes("ISO8859-1"), "GBK");
        } catch (UnsupportedEncodingException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        //System.out.println(params);
        
       
        sysLog.setParams(params);
 
        sysLog.setCreatedTime(new Date());
        
        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes sra = (ServletRequestAttributes) ra;
        HttpServletRequest request = sra.getRequest();
        
        String token = request.getHeader("X-Token");// 从 http 请求头中取出 token
        if(token != null){
            String userCode = JWT.decode(token).getAudience().get(0);
            //获取用户名
            sysLog.setUsername(userCode);
        }
        
        //获取用户ip地址
        sysLog.setIp(IpUtil.getIpAddr(request));
 
        //调用service保存SysLog实体类到数据库
        try {
            //sysLogService.add(sysLog);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println(e.toString());
        }
    }
 
}