为了大家方便使用,以下贴出了所有的代码:
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、数据库自动记录用户的操作日志