最近做项目需要添加一个用户 *** 作记录功能,需要记录用户 *** 作调用了哪些接口。实现方式当然用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.springframework.stereotype.Component;
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) //值越小,越先加载
@Component
public class WebLogAspect {
@Autowired
IJobOperationLogService operationLogService;
private Logger logger = Logger.getLogger(getClass());
ThreadLocal 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();
//通过反射获取到类,填入类名
Class cl1 = Class.forName(joinPoint.getSignature().getDeclaringTypeName());
Method[] methods = cl1.getMethods();
// //获取ApiOperation注解
for(Method method:methods)
{
//joinPoint.getSignature().getName()方法名
if(method.getName().equals(joinPoint.getSignature().getName()))
{
ApiOperation anno = method.getAnnotation(ApiOperation.class);
if (anno != null) {
log.setMethodName(anno.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()));
// }
//需要自己实现获取用户id
public Integer getCurrentUserId(HttpServletRequest request) {
Enumeration auth = request.getHeaders(JwtTokenUtils.TOKEN_HEADER);
String token = auth.nextElement().replace(JwtTokenUtils.TOKEN_PREFIX, STRING_BLANK);
return JwtTokenUtils.getUserId(token);
}
}
保存记录如下
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)