首頁 > 軟體

Spring AOP實現記錄操作紀錄檔

2022-09-02 18:02:16

本文範例為大家分享了Spring AOP實現記錄操作紀錄檔的具體程式碼,供大家參考,具體內容如下

1 新增maven依賴

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

2 自定義操作紀錄檔註解

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface OperationLog {
    //操作型別
    int type() default GlobalConstant.OPERATE_TYPE_QUERY;

    //操作模組
    String module() default "";
}

3 定義切面類

import com.admin.annotation.OperationLog;
import com.admin.sys.dao.SysOperateLogDao;
import com.admin.sys.dao.SysUserDao;
import com.admin.sys.entity.SysOperateLog;
import com.admin.util.JwtUtil;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

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


@Aspect
@Component
public class OperationLogAspect {
    @Resource
    private HttpServletRequest request;

    @Resource
    private SysUserDao sysUserDao;

    @Resource
    private SysOperateLogDao sysOperateLogDao;

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Pointcut("@annotation(com.admin.annotation.OperationLog)")
    public void pointCut() {
    }

    @Around("pointCut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        logger.info("方法執行前");
        //從切面織入點處通過反射機制獲取織入點處的方法
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        logger.info(method.toString());
        //執行方法
        Object obj = joinPoint.proceed();
        OperationLog operationLog = method.getAnnotation(OperationLog.class);
        //通過token獲取使用者ID
        Long userId = JwtUtil.getUserIdByToken(request);
        String userName = this.sysUserDao.selectById(userId).getUserName();
        String operateModule = operationLog.module();
        int operateType = operationLog.type();
        SysOperateLog sysOperateLog = SysOperateLog.builder()
                .userName(userName)
                .operateModule(operateModule)
                .operateType(operateType)
                .build();
        //記錄操作紀錄檔
        this.sysOperateLogDao.insert(sysOperateLog);
        logger.info("方法執行後");
        return obj;
    }
}

4 測試

通過postman傳送請求,如下圖表示記錄操作紀錄檔成功

操作紀錄檔表的sql語句如下

CREATE TABLE `sys_operate_log`  (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `user_name` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL COMMENT '操作者使用者名稱',
  `operate_module` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL COMMENT '操作模組',
  `operate_type` int(1) NULL DEFAULT NULL COMMENT '操作型別(1:查詢  2:插入  3:更改  4:刪除)',
  `operate_time` datetime NULL DEFAULT NULL COMMENT '操作時間',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_bin COMMENT = '操作紀錄檔表' ROW_FORMAT = Dynamic;

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援it145.com。


IT145.com E-mail:sddin#qq.com