AdaKing88
2023-08-23 ae35159387a55199e8ab150ebb97d89d68a235bd
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
package org.jeecg.modules.arj.aop;
 
import com.alibaba.fastjson.JSONObject;
import org.apache.shiro.SecurityUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.IpUtils;
import org.jeecg.common.util.SpringContextUtils;
import org.jeecg.modules.system.entity.SysLog;
import org.jeecg.modules.system.service.ISysLogService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
 
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;
 
/**
 * 切面日志的处理
 * zk
 */
 
@Aspect
@Component
@Order(1)
public class WebLogAspect {
 
    private static Logger logger = LoggerFactory.getLogger(WebLogAspect.class);
 
    @Autowired
    private ISysLogService sysLogService;
 
    /**
     * 定义切入点,切入点为com.example.aop下的所有函数
     */
    @Pointcut("execution(public * org.jeecg.modules.arj.controller..*.*(..))")
    public void webLog() {
    }
 
    /**
     * 前置通知:在连接点之前执行的通知
     */
    @Before("webLog()")
    public void doBefore(JoinPoint joinPoint) throws Exception {
        // 接收到请求,记录请求内容
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        //打印请求内容
        logger.info("请求内容开始 请求地址:{} 请求方式:{}", request.getRequestURL().toString(), request.getMethod());
        logger.info("请求类方法: {} 请求类方法参数: {} ", joinPoint.getSignature(), Arrays.toString(joinPoint.getArgs()));
        SysLog syslog = new SysLog();
        //获取request
        request = SpringContextUtils.getHttpServletRequest();
        //请求的参数
 
        //获取登录用户信息
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        syslog.setCreateTime( new Date());
        syslog.setUsername(sysUser.getUsername());
        syslog.setUserid(sysUser.getUserIdentity().toString());
       //
        syslog.setIp(org.jeecg.modules.arj.util.IpUtils.getIpAddr(request));
        syslog.setLogContent(request.getRequestURL().toString());
        if( request.getRequestURL().toString() != null && request.getRequestURL().toString().indexOf("edit") > -1){
            syslog.setRequestParam(JSONObject.toJSONString(joinPoint.getArgs()));
        }else
        syslog.setRequestParam(JSONObject.toJSONString(joinPoint.getArgs()));
        sysLogService.save(syslog);
 
    }
 
 
    @After("webLog()")
    public void doAfter(JoinPoint joinPoint) throws Exception {
 
    }
 
 
    @AfterReturning(returning = "obj", pointcut = "webLog()")
    public void doAfterReturning(Object obj) throws Throwable {
 
    }
 
}