自定义注解监控接口并记录操作日志到数据库

java 复制代码
package io.renren.ascept;

import io.annotation.LogOperation;
import io.common.utils.HttpContextUtils;
import io.common.utils.IpUtils;
import io.common.utils.JsonUtils;
import io.entity.TokenEntity;
import io.renren.entity.mianchong.ApiLogOperationEntity;
import io.renren.enums.OperationStatusEnum;
import io.renren.service.TokenService;
import io.renren.service.mianchong.ApiLogOperationService;
import org.apache.commons.lang3.StringUtils;
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.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Date;

/**
 * @describe 记录操作日志
 */
@Aspect
@Component
public class LogOperationAspect {
    @Resource
    private ApiLogOperationService apiLogOperationService;
    @Resource
    private TokenService tokenService;

    @Pointcut("@annotation(io.renren.annotation.LogOperation)")
    public void logPointCut() {
    }

    @Around("logPointCut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        long beginTime = System.currentTimeMillis();
        try {
            //执行方法
            Object result = point.proceed();
            //执行时长(毫秒)
            long time = System.currentTimeMillis() - beginTime;
            //保存日志
            saveLog(point, time, OperationStatusEnum.SUCCESS.value());
            return result;
        } catch (Exception e) {
            //执行时长(毫秒)
            long time = System.currentTimeMillis() - beginTime;
            //保存日志
            saveLog(point, time, OperationStatusEnum.FAIL.value());
            throw e;
        }
    }

    private void saveLog(ProceedingJoinPoint joinPoint, long time, Integer status) throws Exception {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = joinPoint.getTarget().getClass().getDeclaredMethod(signature.getName(), signature.getParameterTypes());
        LogOperation annotation = method.getAnnotation(LogOperation.class);

        ApiLogOperationEntity log = new ApiLogOperationEntity();
        if (annotation != null) {
            //注解上的描述
            log.setOperation(annotation.value());
        }

        log.setStatus(status);
        log.setRequestTime((int) time);

        //请求相关信息
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
        assert request != null;

        //客户端ip
        log.setIp(IpUtils.getClientIpAddress(request));

        //从header中获取token
        String token = request.getHeader("token");
        //token为空
        if (StringUtils.isBlank(token)) {
            log.setUserAgent("未登录操作");
        } else {
            //查询token信息
            TokenEntity tokenEntity = tokenService.getByToken(token);
            //登录用户信息
            log.setUserAgent(tokenEntity.getUserId().toString());
        }
        log.setRequestUri(request.getRequestURI());
        log.setRequestMethod(request.getMethod());

        //请求参数
        Object[] args = joinPoint.getArgs();
        try {
            String params = JsonUtils.toJsonString(args[0]);
            log.setRequestParams(params);
        } catch (Exception ignored) {
        }

        log.setCreateDate(new Date());
        //保存到DB
        apiLogOperationService.save(log);
    }
}
java 复制代码
package io.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * 操作日志注解
 *
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface LogOperation {

    String value() default "";
}

使用方法

java 复制代码
@PostMapping("xxxxx")
    @LogOperation("接口描述")
    public ResponseEntity<?> xxxxxxxx(@RequestBody DTO dto) throws IOException {
    ..............
    }

操作日志表:

sql 复制代码
CREATE TABLE `db_name`.`log_operation`  (
  `id` bigint(0) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'id',
  `operation` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '用户操作',
  `request_uri` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '请求URI',
  `request_method` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '请求方式',
  `request_params` text CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL COMMENT '请求参数',
  `request_time` int(0) UNSIGNED NOT NULL COMMENT '请求时长(毫秒)',
  `user_agent` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '用户代理',
  `ip` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '操作IP',
  `status` tinyint(0) UNSIGNED NOT NULL COMMENT '状态  0:失败   1:成功',
  `creator_name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '用户名',
  `creator` bigint(0) NULL DEFAULT NULL COMMENT '创建者',
  `create_date` datetime(0) NULL DEFAULT NULL COMMENT '创建时间',
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `idx_create_date`(`create_date`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '操作日志' ROW_FORMAT = Dynamic;
相关推荐
申城异乡人2 分钟前
Spring RestTemplate使用方法总结
java
Microsoft Word7 分钟前
分布式数据库HBase
数据库·分布式·hbase
独立开阀者_FwtCoder22 分钟前
不要再像我这样使用 React 导入了,试试 Wrapper 模式吧!
前端·javascript·数据库
有诺千金24 分钟前
深入理解 Spring Boot 的@AutoConfiguration注解
java·spring boot·后端
代码吐槽菌25 分钟前
基于SpringBoot的律师事务所案件管理系统【附源码】
java·数据库·spring boot·后端·毕业设计
flzjkl31 分钟前
【源码】【Java并发】【ReentrantLock】适合中学者体质的ReentrantLock源码阅读
java·后端
freejackman34 分钟前
Java 环境配置
java·后端·java ee
用户24003619235001 小时前
mysql 索引的初步认识
数据库
bug菌1 小时前
领导安排我一小时实现一个导出功能,我竟用@Excel注解两分钟搞定!🫠
java·后端·spring
bug菌1 小时前
Swagger注解全攻略:一文搞懂 @ApiIgnore 的妙用!
java·后端·spring