使用springcloud 和springboot没有多大区别,主要是关于aop的代码要放在公共项目common中,一开始我放在某个业务工程t1中,其他没有依赖t1的工程,都不能使用该log。
该日志的功能: 记录用户每一次的行为的用户ID,使用时间,请求参数,返回结果,模块,请求时长。【将该日志数据存到数据库中,能够让cloud项目所有的controller接口都能使用,不用自定义注解】
使用了环绕通知
=====================================================
代码:
1.日志实体类:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18@Data @ToString @AllArgsConstructor @NoArgsConstructor public class SysLog implements Serializable { private Integer id; private String username; private String operation; private String time; private String method; private String params; private String ip; // @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8") private String createTime; private String result; }
2.获取用户IP地址:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21public class IPUtils { /** * 获取IP地址 * * 使用Nginx等反向代理软件, 则不能通过request.getRemoteAddr()获取IP地址 * 如果使用了多级反向代理的话,X-Forwarded-For的值并不止一个,而是一串IP地址,X-Forwarded-For中第一个非unknown的有效IP字符串,则为真实IP地址 */ public static String getIpAddr(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.getRemoteAddr(); } return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : ip; } }
3.
复制代码
1
2
3
4
5
6public class HttpContextUtils { public static HttpServletRequest getHttpServletRequest() { return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); } }
4.正文:
复制代码
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@Slf4j @Aspect @Component public class LogAspects { // @Autowired // private SysLogDao sysLogDao; // @Pointcut("@annotation(com.lezhi.course.config.log.Log)") // public void pointcut() { } // @Around("pointcut()") @Around("execution(* com.lezhi.*.controller.*.*(..))") public Object around(ProceedingJoinPoint point) { Object result = null; long beginTime = System.currentTimeMillis(); try { // 执行方法 result = point.proceed(); log.error("返回值为:"+ JSON.toJSONString(result)); } catch (Throwable e) { e.printStackTrace(); } // 执行时长(毫秒) long time = System.currentTimeMillis() - beginTime; // 保存日志 saveLog(point, time,result); return result; } private void saveLog(ProceedingJoinPoint joinPoint, long time,Object result ) { MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod(); SysLog sysLog = new SysLog(); Log logAnnotation = method.getAnnotation(Log.class); // if (logAnnotation != null) { // // 注解上的描述 // sysLog.setOperation(logAnnotation.value()); // } // 请求的方法名 String className = joinPoint.getTarget().getClass().getName(); String methodName = signature.getName(); sysLog.setMethod(className + "." + methodName + "()"); // 请求的方法参数值 Object[] args = joinPoint.getArgs(); // 请求的方法参数名称 LocalVariableTableParameterNameDiscoverer u = new LocalVariableTableParameterNameDiscoverer(); String[] paramNames = u.getParameterNames(method); if (args != null && paramNames != null) { String params = ""; for (int i = 0; i < args.length; i++) { params += " " + paramNames[i] + ": " + args[i]; } sysLog.setParams(params); } // 获取request HttpServletRequest request = HttpContextUtils.getHttpServletRequest(); // 设置IP地址 sysLog.setIp(IPUtils.getIpAddr(request)); // 模拟一个用户名 SecurityUserDetails userDetails = (SecurityUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); Long userId = Long.parseLong(userDetails.getUsername()); sysLog.setUserId(userId); sysLog.setTime( String.valueOf(time)+"毫秒"); //设置日期格式 HH:mm:ss中的HH大写为24小时制。HH和hh的差别是前者为24小时制,后者为12小时制 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // new Date()为获取当前系统时间 String dateTime=df.format(new Date()); sysLog.setCreateTime(dateTime); sysLog.setResult( JSON.toJSONString(result)); log.error("新增的log日志::"+sysLog.toString()); // 保存系统日志 // sysLogDao.saveSysLog(sysLog); } }
最后
以上就是落寞大山最近收集整理的关于Spring Cloud使用 AOP记录用户操作日志的全部内容,更多相关Spring内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复