700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > java AOP实现记录操作日志

java AOP实现记录操作日志

时间:2022-06-02 19:31:19

相关推荐

java AOP实现记录操作日志

最近做项目需要添加一个用户操作记录功能,需要记录用户操作调用了哪些接口,通过获取swagger注解上的接口说明,然后存到数据库,也可以不落库打印日志记录。

数据库:

CREATE TABLE `job_operation_log` (`id` bigint(20) NOT NULL AUTO_INCREMENT,`url` varchar(255) DEFAULT NULL COMMENT '请求接口地址',`http_method` varchar(255) DEFAULT NULL COMMENT '请求方式',`ip` varchar(30) DEFAULT NULL COMMENT 'ip地址',`class_method` varchar(255) DEFAULT NULL COMMENT 'class方法',`create_time` datetime DEFAULT NULL,`user_id` int(11) DEFAULT NULL COMMENT '用户id',`method_name` varchar(50) DEFAULT NULL COMMENT '方法说明',PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=97 DEFAULT CHARSET=utf8mb4 COMMENT='操作日志';

import com.wugui.datax.admin.entity.JobOperationLog;import com.wugui.datax.admin.service.IJobOperationLogService;import com.wugui.datax.admin.util.JwtTokenUtils;import io.swagger.annotations.ApiOperation;import org.apache.log4j.Logger;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.annotation.AfterReturning;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.core.annotation.Order;import org.ponent;import org.springframework.web.context.request.RequestContextHolder;import org.springframework.web.context.request.ServletRequestAttributes;import javax.servlet.http.HttpServletRequest;import java.lang.reflect.Method;import java.util.Arrays;import java.util.Date;import java.util.Enumeration;import static com.wugui.datatx.core.util.Constants.STRING_BLANK;/*** Web层日志切面*/@Aspect //这里使用@Aspect注解方式设置AOP@Order(5) //值越小,越先加载@Componentpublic class WebLogAspect {@AutowiredIJobOperationLogService operationLogService;private Logger logger = Logger.getLogger(getClass());ThreadLocal<Long> startTime = new ThreadLocal<>();//这里@Pointcut设置切点可以设置为Controller层的地址@Pointcut("execution( * com.wugui.datax.admin.controller.*.*(..))")public void webLog() {}//@Before指在切点方法之前执行,也就是在Controller层方法执行之前执行,这里可以通过JoinPoint获取一些有关方法的信息,在这里也可以修改参数的值//@Before()括号里设置的是切点方法的名称@Before("webLog()")public void doBefore(JoinPoint joinPoint) throws Throwable {// 接收到请求,记录请求内容ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();HttpServletRequest request = attributes.getRequest();//操作日志对象JobOperationLog log = new JobOperationLog();//将目标方法的签名对象转化为MethodSignatureMethodSignature methodSignature= (MethodSignature) joinPoint.getSignature();//获得方法的注解Method method = methodSignature.getMethod();ApiOperation anno = method.getDeclaredAnnotation(ApiOperation.class);String value = anno.value();log.setMethodName(value);log.setUrl(request.getRequestURL().toString());log.setIp(request.getRemoteAddr());log.setHttpMethod(request.getMethod());log.setUserId(getCurrentUserId(request));log.setCreateTime(new Date());log.setClassMethod(joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());operationLogService.save(log);}// @AfterReturning(returning = "ret", pointcut = "webLog()")// public void doAfterReturning(Object ret) throws Throwable {// // 处理完请求,返回内容// logger.info("RESPONSE : " + ret);// logger.info("SPEND TIME : " + (System.currentTimeMillis() - startTime.get()));// }//需要自己实现获取用户idpublic Integer getCurrentUserId(HttpServletRequest request) {Enumeration<String> auth = request.getHeaders(JwtTokenUtils.TOKEN_HEADER);String token = auth.nextElement().replace(JwtTokenUtils.TOKEN_PREFIX, STRING_BLANK);return JwtTokenUtils.getUserId(token);}}

保存记录如下

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。