springboot项目自定义切面增强方法功能(springboot记录日志)

说明

背景:记录系统接口日志入库,包含接口方法、入参、回参、响应时间、操作人、操作时间等信息。

方案:添加自定义切面处理

一、自定义切面注解

java 复制代码
package com.gstanzer.supervise.annotation;

import com.gstanzer.supervise.enums.BusinessType;

import java.lang.annotation.*;

/**
 * 系统操作日志
 *
 * @author: tangbingbing
 * @date: 2023/12/15 15:25
 */
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface SysOperLog {
    /**
     * 接口名称
     */
    String name();

    /**
     * 接口功能
     */
    BusinessType businessType() default BusinessType.OTHER;

}

二、自定义切面

java 复制代码
package com.gstanzer.supervise.aspect;


import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.gstanzer.supervise.annotation.SysOperLog;
import com.gstanzer.supervise.mapper.SysOperLogMapper;
import com.gstanzer.supervise.po.SysOperLogPO;
import com.gstanzer.supervise.utils.IpUtils;
import com.gstanzer.supervise.utils.IpUtilsNew;
import com.gstanzer.supervise.utils.ServletUtils;
import com.gstanzer.supervise.utils.TokenUtils;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;
import org.springframework.validation.BindingResult;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Collection;
import java.util.Date;
import java.util.Map;

/**
 * 系统操作日志记录处理
 *
 * @author: tangbingbing
 * @date: 2023/12/15 15:25
 */
@Aspect
@Component
public class SysOperLogAspect {
    private static final Logger log = LoggerFactory.getLogger(SysOperLogAspect.class);

    ThreadLocal<Long> startTime = new ThreadLocal<>();

    @Resource
    private SysOperLogMapper sysOperLogMapper;

    @Before("@annotation(controllerLog)")
    public void doBefore(JoinPoint joinPoint, SysOperLog controllerLog) {
        //开始计时
        startTime.set(System.currentTimeMillis());
    }

    /**
     * 处理完请求后执行
     *
     * @param joinPoint 切点
     */
    @AfterReturning(pointcut = "@annotation(controllerLog)", returning = "jsonResult")
    public void doAfterReturning(JoinPoint joinPoint, SysOperLog controllerLog, Object jsonResult) {
        HttpServletRequest request = IpUtils.getHttpServletRequest();
        handleLog(joinPoint, controllerLog, null, jsonResult, request);
    }

    /**
     * 拦截异常操作
     *
     * @param joinPoint 切点
     * @param e         异常
     */
    @AfterThrowing(value = "@annotation(controllerLog)", throwing = "e")
    public void doAfterThrowing(JoinPoint joinPoint, SysOperLog controllerLog, Exception e) {
        HttpServletRequest request = IpUtils.getHttpServletRequest();
        handleLog(joinPoint, controllerLog, e, null, request);
    }

    protected void handleLog(final JoinPoint joinPoint, SysOperLog controllerLog, final Exception e, Object jsonResult, HttpServletRequest request) {
        try {
            long totalRuntime = System.currentTimeMillis() - startTime.get();
            String userId = "";
            String loginName = "";
            Date date = new Date();
            String token = request.getHeader("gst-token");
            Map<String, String> userMap = TokenUtils.getUserInfoByToken(token);
            if (userMap != null) {
                userId = userMap.get("userId");
                loginName = userMap.get("loginName");
            }
            SysOperLogPO operLog = new SysOperLogPO();
            String ip = IpUtilsNew.getIpAddr(ServletUtils.getRequest());
            operLog.setId(String.valueOf(IdUtil.getSnowflake(1,1).nextId()));
            operLog.setOperIp(ip);
            operLog.setOperUrl(StringUtils.substring(ServletUtils.getRequest().getRequestURI(), 0, 255));
            operLog.setUserId(userId);
            operLog.setUserName(loginName);
            if (e != null) {
                operLog.setErrorMsg(e.getMessage());
            }
            //String className = joinPoint.getTarget().getClass().getName();
            String methodName = joinPoint.getSignature().getName();
            operLog.setMethodName(methodName);
            operLog.setOperTime(date);
            operLog.setResponseTime(totalRuntime + "ms");
            // 设置请求方式
            operLog.setRequestMethod(ServletUtils.getRequest().getMethod());
            // 处理设置注解上的参数
            getControllerMethodDescription(joinPoint, controllerLog, operLog, jsonResult);
            // 保存数据库
            log.info("操作日志入库:{}", operLog);
            sysOperLogMapper.insert(operLog);
        } catch (Exception exp) {
            // 记录本地异常日志
            log.error("==前置通知异常==");
            log.error("异常信息:{}", exp.getMessage());
            exp.printStackTrace();
        }
    }

    /**
     * 获取注解中对方法的描述信息 用于Controller层注解
     *
     * @param log     日志
     * @param operLog 操作日志
     * @throws Exception
     */
    public void getControllerMethodDescription(JoinPoint joinPoint, SysOperLog log, SysOperLogPO operLog, Object jsonResult) throws Exception {
        operLog.setMethodName(log.name());
        operLog.setBusinessType(String.valueOf(log.businessType()));
        setRequestValue(joinPoint, operLog);
        operLog.setResponseParam(JSON.toJSONString(jsonResult));
    }

    /**
     * 获取请求的参数,放到log中
     *
     * @param operLog 操作日志
     * @throws Exception 异常
     */
    private void setRequestValue(JoinPoint joinPoint, SysOperLogPO operLog) throws Exception {
        String requestMethod = operLog.getRequestMethod();
        if (HttpMethod.PUT.name().equals(requestMethod) || HttpMethod.POST.name().equals(requestMethod)) {
            String params = argsArrayToString(joinPoint.getArgs());
            operLog.setRequestParam(params);
        }
    }

    /**
     * 参数拼装
     */
    private String argsArrayToString(Object[] paramsArray) {
        String params = "";
        if (paramsArray != null && paramsArray.length > 0) {
            for (Object o : paramsArray) {
                if (o != null && !isFilterObject(o)) {
                    try {
                        String jsonObj = JSON.toJSONString(o);
                        params += jsonObj + " ";
                    } catch (Exception e) {
                    }
                }
            }
        }
        return params.trim();
    }

    /**
     * 判断是否需要过滤的对象。
     *
     * @param o 对象信息。
     * @return 如果是需要过滤的对象,则返回true;否则返回false。
     */
    @SuppressWarnings("rawtypes")
    public boolean isFilterObject(final Object o) {
        Class<?> clazz = o.getClass();
        if (clazz.isArray()) {
            return clazz.getComponentType().isAssignableFrom(MultipartFile.class);
        } else if (Collection.class.isAssignableFrom(clazz)) {
            Collection collection = (Collection) o;
            for (Object value : collection) {
                return value instanceof MultipartFile;
            }
        } else if (Map.class.isAssignableFrom(clazz)) {
            Map map = (Map) o;
            for (Object value : map.entrySet()) {
                Map.Entry entry = (Map.Entry) value;
                return entry.getValue() instanceof MultipartFile;
            }
        }
        return o instanceof MultipartFile || o instanceof HttpServletRequest || o instanceof HttpServletResponse
                || o instanceof BindingResult;
    }
}

三、在需要记录日志的接口上加上该注解即可

相关推荐
苹果醋333 分钟前
React源码02 - 基础知识 React API 一览
java·运维·spring boot·mysql·nginx
Hello.Reader1 小时前
深入解析 Apache APISIX
java·apache
盛派网络小助手1 小时前
微信 SDK 更新 Sample,NCF 文档和模板更新,更多更新日志,欢迎解锁
开发语言·人工智能·后端·架构·c#
菠萝蚊鸭1 小时前
Dhatim FastExcel 读写 Excel 文件
java·excel·fastexcel
旭东怪1 小时前
EasyPoi 使用$fe:模板语法生成Word动态行
java·前端·word
007php0071 小时前
Go语言zero项目部署后启动失败问题分析与解决
java·服务器·网络·python·golang·php·ai编程
∝请叫*我简单先生2 小时前
java如何使用poi-tl在word模板里渲染多张图片
java·后端·poi-tl
ssr——ssss2 小时前
SSM-期末项目 - 基于SSM的宠物信息管理系统
java·ssm
一棵星2 小时前
Java模拟Mqtt客户端连接Mqtt Broker
java·开发语言
鲤籽鲲2 小时前
C# Random 随机数 全面解析
android·java·c#