

我使用Spring AOP实现了用户操作日志功能
source link: https://blog.51cto.com/u_15323393/5454211
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

我使用Spring AOP实现了用户操作日志功能
推荐 原创今天答辩完了,复盘了一下系统,发现还是有一些东西值得拿出来和大家分享一下。
系统需要对用户的操作进行记录,方便未来溯源
首先想到的就是在每个方法中,去实现记录的逻辑,但是这样做肯定是不现实的,首先工作量大,其次违背了软件工程设计原则(开闭原则)
这种需求显然是对代码进行增强,首先想到的是使用 SpringBoot 提供的 AOP 结合注解的方式来实现
1、 需要一张记录日志的 Log 表

导出的 sql 如下:
-- mcams.t_log definition
CREATE TABLE `t_log` (
`log_id` int NOT NULL AUTO_INCREMENT COMMENT '日志编号',
`user_id` int NOT NULL COMMENT '操作人id',
`operation` varchar(128) NOT NULL COMMENT '用户操作',
`method` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '操作的方法',
`params` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '方法的参数',
`ip` varchar(40) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '用户的ip',
`create_time` timestamp NULL DEFAULT NULL COMMENT '操作时间',
`cost_time` int DEFAULT NULL COMMENT '花费时间',
PRIMARY KEY (`log_id`)
) ENGINE=InnoDB AUTO_INCREMENT=189 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
2、我使用的是 Spring Boot 所以需要引入 spring aop 的 starter #
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
如果是使用 spring 框架的,引入 spring-aop 即可
3、Log 实体类 #
package com.xiaofengstu.mcams.web.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable;
import java.time.LocalDateTime;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Getter;
import lombok.Setter;
/**
* <p>
*
* </p>
*
* @author fengzeng
* @since 2022-05-21
*/
@Getter
@Setter
@TableName("t_log")
public class TLog implements Serializable {
private static final long serialVersionUID = 1L;
@TableId(value = "log_id", type = IdType.AUTO)
private Integer logId;
/**
* 操作人id
*/
@TableField("user_id")
private Integer userId;
/**
* 用户操作
*/
@TableField("operation")
private String operation;
@TableField("method")
private String method;
@TableField("params")
private String params;
@TableField("ip")
private String ip;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@TableField("create_time")
private LocalDateTime createTime;
@TableField("cost_time")
private Long costTime;
}
需要 lombok 插件(@getter && @setter 注解)
4、ILog 注解 #
package com.xiaofengstu.mcams.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @Author FengZeng
* @Date 2022-05-21 00:48
* @Description TODO
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ILog {
String value() default "";
}
5、切面类 LogAspect #
package com.xiaofengstu.mcams.aspect;
import com.xiaofengstu.mcams.annotation.ILog;
import com.xiaofengstu.mcams.util.ThreadLocalUtils;
import com.xiaofengstu.mcams.web.entity.TLog;
import com.xiaofengstu.mcams.web.service.TLogService;
import lombok.RequiredArgsConstructor;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
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.lang.reflect.Method;
import java.time.LocalDateTime;
/**
* @Author FengZeng
* @Date 2022-05-21 00:42
* @Description TODO
*/
@Aspect
@Component
@RequiredArgsConstructor
public class LogAspect {
private final TLogService logService;
@Pointcut("@annotation(com.xiaofengstu.mcams.annotation.ILog)")
public void pointcut() {
}
@Around("pointcut()")
public Object around(ProceedingJoinPoint point) {
Object result = null;
long beginTime = System.currentTimeMillis();
try {
result = point.proceed();
} catch (Throwable e) {
e.printStackTrace();
}
long costTime = System.currentTimeMillis() - beginTime;
saveLog(point, costTime);
return result;
}
private void saveLog(ProceedingJoinPoint point, long costTime) {
// 通过 point 拿到方法签名
MethodSignature methodSignature = (MethodSignature) point.getSignature();
// 通过方法签名拿到被调用的方法
Method method = methodSignature.getMethod();
TLog log = new TLog();
// 通过方法区获取方法上的 ILog 注解
ILog logAnnotation = method.getAnnotation(ILog.class);
if (logAnnotation != null) {
log.setOperation(logAnnotation.value());
}
String className = point.getTarget().getClass().getName();
String methodName = methodSignature.getName();
log.setMethod(className + "." + methodName + "()");
// 获取方法的参数
Object[] args = point.getArgs();
LocalVariableTableParameterNameDiscoverer l = new LocalVariableTableParameterNameDiscoverer();
String[] parameterNames = l.getParameterNames(method);
if (args != null && parameterNames != null) {
StringBuilder param = new StringBuilder();
for (int i = 0; i < args.length; i++) {
param.append(" ").append(parameterNames[i]).append(":").append(args[i]);
}
log.setParams(param.toString());
}
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
log.setIp(request.getRemoteAddr());
log.setUserId(ThreadLocalUtils.get());
log.setCostTime(costTime);
log.setCreateTime(LocalDateTime.now());
logService.save(log);
}
}
因为我使用的是 Mybatis-plus,所以 logService.save(log);
是 mybatis-plus 原生的 save operation
这步其实就是把 log 插入到数据库
只需要在方法上加上 @ILog 注解,并设置它的 value 即可(value 就是描述当前 method 作用)
package com.xiaofengstu.mcams.web.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaofengstu.mcams.annotation.ILog;
import com.xiaofengstu.mcams.dto.BasicResultDTO;
import com.xiaofengstu.mcams.enums.RespStatusEnum;
import com.xiaofengstu.mcams.web.entity.TDept;
import com.xiaofengstu.mcams.web.service.TDeptService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* <p>
* 前端控制器
* </p>
*
* @author fengzeng
* @since 2022-05-07
*/
@RestController
@RequestMapping("/web/dept")
@RequiredArgsConstructor
public class TDeptController {
private final TDeptService deptService;
@ILog("获取部门列表")
@GetMapping("/list")
public BasicResultDTO<List<TDept>> getDeptListByCampId(@RequestParam("campusId") Integer campusId) {
return new BasicResultDTO(RespStatusEnum.SUCCESS, deptService.list(new QueryWrapper<TDept>().eq("campus_id", campusId)));
}
@ILog("通过角色获取部门列表")
@GetMapping("/listByRole")
public BasicResultDTO<List<TDept>> getDeptListByRole() {
return new BasicResultDTO<>(RespStatusEnum.SUCCESS, deptService.listByRole());
}
}

如果要对现有代码进行功能扩展,使用 AOP + 注解不妨为一种优雅的方式
对 AOP 不熟悉的小伙伴,可以深入了解一下,毕竟是 spring 最重要的特性之一。
Recommend
-
53
休息日闲着无聊看了下 Spring Boot 中的日志实现,把我的理解跟大家说下。 门面模式 说到日志框架不得不说门面模式。门面...
-
10
本篇要点 简要回顾S pringAOP的相关知识点:关键术语,通知类型,切入点表达式等等。 ...
-
6
【基础系列】Spring之AOP结合SpEL实现日志输出的注意事项
-
5
最简单的Spring Boot 整合ELK教程,实现日志收集 ...
-
7
Golang 埋点诊断功能Permalink 研发一段代码之后,我们需要测试和调试它。 关于...
-
3
【MyBatis】MyBatis配置、使用MyBatis操作数据库、开启SQL日志打印 推荐 原创
-
6
【Logback+Spring-Aop】实现全面生态化的全链路日志追踪系统服务插件「Logback-MDC篇」 精选 原创 洛神灬...
-
5
【Logback+Spring-Aop】实现全面生态化的全链路日志追踪系统服务插件「Logback-MDC篇」 - 洛神灬殇 - 博客园 日志追踪对于功能问题的排查和数据流转的...
-
8
在Spring Boot中用Loki实现日志记录 解道Jdon ...
-
6
在现代Web应用程序中,记录用户系统操作流程对于监控用户行为、进行故障排查、安全审计等方面都是非常重要的。在本篇博客中,我们将介绍如何在Spring Boot中使用AOP(面向切面编程)和日志框架来实现用户系统操作流程的记录。
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK