利用spring的aop、自定義註解動態記錄用戶操作日誌

爲了大家方便使用,以下貼出了所有的代碼:

1、追加aop依賴

        <!--spring切面aop依賴-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

2、自定義註解

package com.jndj.platform.common.annotation;

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

/**
 * 操作日誌(增加、編輯、刪除)
 *
 * @author yaohj
 * @date 2020-06-12
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface LoggerOperator {
    // 操作內容
    String content() default "";
}

3、定義切點 @Pointcut、在註解的位置切入代碼

package com.jndj.platform.common.aspect;

import com.jndj.platform.common.annotation.LoggerOperator;
import com.jndj.platform.common.util.DateUtils;
import com.jndj.platform.system.operateLog.entity.OperateLog;
import com.jndj.platform.system.operateLog.service.OperateLogService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
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 javax.servlet.http.HttpSession;
import java.lang.reflect.Method;

/**
 * 操作日誌:切面處理類
 */
@Aspect
@Component
public class OperateLogAspect {

    @Autowired
    private OperateLogService operateLogService;

    // 定義切點 @Pointcut
    // 在註解的位置切入代碼
    @Pointcut("@annotation(com.jndj.platform.common.annotation.LoggerOperator)")
    public void operateLogPointCut() {
    }

    // 配置切面通知
    @AfterReturning("operateLogPointCut()")
    public void saveOperateLog(JoinPoint joinPoint) {
        //保存日誌
        OperateLog operateLog = new OperateLog();

        // 從切面植入點處通過反射機制獲取植入點處的方法
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        //獲取切入點所在的方法
        Method method = signature.getMethod();

        // 獲取操作
        LoggerOperator userOperateLog = method.getAnnotation(LoggerOperator.class);
        if (userOperateLog != null) {
            String content = userOperateLog.content();
            operateLog.setOperateContent(content);  //保存獲取的操作內容
        }

        // 獲取請求的類名、方法名
        String className = joinPoint.getTarget().getClass().getName();
        String methodName = method.getName();
        operateLog.setOperate(className + "." + methodName); //保存獲取的類名、方法名

        // 獲取請用的用戶名
        HttpServletRequest request =
                ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        HttpSession session = request.getSession(true);
        if (session != null) {
            String userName = (String)session.getAttribute("username");
            operateLog.setUsername(userName);
        }
        // 記錄操作時間
        operateLog.setTime(DateUtils.getCurrDateTimeStamp());

        //調用service保存實體類到數據庫
        operateLogService.save(operateLog);
    }
}

4、在controller的方法上使用註解@LoggerOperator就可以了,非常方便

 /**
  * 登錄
  */
 @LoggerOperator(content = "用戶登錄")
 @GetMapping(API_PREFIX + PATH + "/login")
 public ResponseEntity loginGo(HttpServletRequest request,String userName, String password) {
     Subject currentUser = SecurityUtils.getSubject();
     // base64密碼解密
     Base64.Decoder decoder = Base64.getDecoder();
     String newpassword = new String(decoder.decode(password));
     UsernamePasswordToken token = new UsernamePasswordToken(userName, newpassword);
		// 此處省略若干行
 }

5、數據庫自動記錄用戶的操作日誌
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章