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;
    }
}

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

相关推荐
xmh-sxh-13149 分钟前
jdk各个版本介绍
java
XINGTECODE22 分钟前
海盗王集成网关和商城服务端功能golang版
开发语言·后端·golang
天天扭码28 分钟前
五天SpringCloud计划——DAY2之单体架构和微服务架构的选择和转换原则
java·spring cloud·微服务·架构
程序猿进阶28 分钟前
堆外内存泄露排查经历
java·jvm·后端·面试·性能优化·oom·内存泄露
FIN技术铺33 分钟前
Spring Boot框架Starter组件整理
java·spring boot·后端
小曲程序40 分钟前
vue3 封装request请求
java·前端·typescript·vue
凡人的AI工具箱1 小时前
15分钟学 Go 第 60 天 :综合项目展示 - 构建微服务电商平台(完整示例25000字)
开发语言·后端·微服务·架构·golang
陈王卜1 小时前
django+boostrap实现发布博客权限控制
java·前端·django
小码的头发丝、1 小时前
Spring Boot 注解
java·spring boot
先天牛马圣体1 小时前
如何提升大型AI模型的智能水平
后端