1.注解接口:
import com.github.wxiaoqi.security.common.constant.Constants;

import java.lang.annotation.*;

/**
* 日志注解
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SysLogOpt {
String value();

String module() default ""; //模块名称 系统管理-用户管理-列表页面

String description() default ""; //描述

Constants.LogOptEnum operationType() default Constants.LogOptEnum.UNKNOW;//操作类型
}

2.切面类
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.duiyue.common.mvc.core.APIResponse;
import com.duiyue.common.util.LogUtils;
import com.github.wxiaoqi.security.admin.annotation.SysLogOpt;
import com.github.wxiaoqi.security.admin.biz.SysLogService;
import com.github.wxiaoqi.security.api.entity.SysLog;
import com.github.wxiaoqi.security.common.constant.Constants;
import com.github.wxiaoqi.security.common.context.BaseContextHandler;
import com.github.wxiaoqi.security.common.util.RegexUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
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.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

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

@Slf4j
@Aspect
@Component
public class LogAspect {
/**
* 开始时间
*/
private long startTime = 0L;

@Resource
private SysLogService logService;

@Pointcut("execution(* *..rest..*.*(..)) && @annotation(com.github.wxiaoqi.security.admin.annotation.SysLogOpt)")
public void logPointCut() {

}

@Around("logPointCut()")
public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
startTime = System.currentTimeMillis();
Object result = null;
SysLog sysLogModel = logPre(pjp);
try {
result = pjp.proceed();
} finally {
//查询类型不添加日志
if (Constants.LogOptEnum.QUERY.value() != sysLogModel.getOperationType()) {
if (logAfter(result, sysLogModel).getUserName() != null) {
sysLogModel.setCreateTime(new Date());
sysLogModel.setUpdateTime(new Date());
logService.add(sysLogModel);
}
}
}
return result;
}

private SysLog logPre(ProceedingJoinPoint pjp) throws Exception {
SysLog sysLogModel = new SysLog();
for (Method method : Class.forName(pjp.getTarget().getClass().getName()).getMethods()) {
if (method.getName().equals(pjp.getSignature().getName())) {
Class[] clazzs = method.getParameterTypes();
if (clazzs.length == pjp.getArgs().length) {
//方法名称
sysLogModel.setOperation(method.getAnnotation(SysLogOpt.class).value());
//操作类型
sysLogModel.setOperationType(method.getAnnotation(SysLogOpt.class).operationType().value());
break;
}
}
}
//获取请求对象
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
startTime = System.currentTimeMillis();
String ip = HttpUtil.getClientIP(request);
//方法名含包名(om.github.wxiaoqi.security.admin.rest.SysLogController.queryListPage)
String classMethod = pjp.getSignature().getDeclaringTypeName() + "." + pjp.getSignature().getName();
//请求参数
String args = JSON.toJSONString(pjp.getArgs()).replaceAll(RegexUtil.getJSonValueRegex("password"), "****").replaceAll(RegexUtil.getJSonValueRegex("oldPassword"), "****");

sysLogModel.setIp(ip);
sysLogModel.setMethod(classMethod);
sysLogModel.setParams(args);
sysLogModel.setCreateTime(new Date());
sysLogModel.setCreateUser(0L);
sysLogModel.setUpdateUser(0L);

String currentUserName = "";
try {
currentUserName = BaseContextHandler.getUsername();
} catch (Exception e) {
LogUtils.error(log, "", e);
}
if (StringUtils.isNotEmpty(currentUserName)) {
sysLogModel.setUserName(currentUserName);
}
return sysLogModel;
}

private SysLog logAfter(Object result, SysLog sysLogModel) {
APIResponse response = null;
if (result != null) {
response = (APIResponse) result;
}
String currentUserName = "";
try {
currentUserName = BaseContextHandler.getUsername();
} catch (Exception e) {
LogUtils.error(log, "", e);
}
if (StringUtils.isNotEmpty(currentUserName)) {
sysLogModel.setUserName(currentUserName);
}

//返回结果
if (response != null && response.getStatus() == Constants.ResultCodeEnum.SUCCESS.value()) {
sysLogModel.setResult(1);
} else {
sysLogModel.setResult(0);
}

//执行时长(毫秒)
Long spendTime = System.currentTimeMillis() - startTime;
sysLogModel.setProcessTime(spendTime);
return sysLogModel;
}
}

3.控制层使用(方法上使用)。
@SysLogOpt(module = "活动管理", value = "添加活动", operationType = Constants.LogOptEnum.ADD)

(注:枚举类型未给出,自行修改)

最新文章

  1. winddows 运行指令 (2)
  2. linux 常用命令及技巧
  3. React-Native学习指南
  4. Chapter 5. The Gradle Wrapper 关于gradle wrapper
  5. JavaScript中的memorizing技术
  6. C语言必背18个经典程序
  7. DELPHI 任务栏无EXE显示
  8. Asp.Net Core 中无法使用 ConfigurationManager.AppSettings
  9. 解决使用BottomSheetDialog时状态栏变黑的问题
  10. java.util中,util是什么意思
  11. MySQL 多表结构的创建与分析
  12. 为什么需要提前撰写Spec文档
  13. Perl包相关
  14. 一套简单的git版本控制代码
  15. 随机漂浮图片、右侧上下浮动快捷栏JS
  16. Python调用C++类
  17. XPATH语法(一)
  18. json-lib使用——JSONObject与JSONArray
  19. 【Nginx】修改响应头,根据不同请求IP重定向到不同IP
  20. Tomcat 之 启动tomcat时 错误: 代理抛出异常 : java.rmi.server.ExportException: Port already in use: 1099;

热门文章

  1. 攻防世界 WEB 高手进阶区 easytornado Writeup
  2. hivesql笔记
  3. JAVA线上常见问题排查手段(小结)
  4. Spring Cloud Gateway实战之四:内置predicate小结
  5. kubernetes基本概念 pod, service
  6. Jenkins教程(八)实现 GitLab 触发 Jenkins 自动按模块发布前端
  7. [luogu1390]公约数的和
  8. spring security 认证源码跟踪
  9. 对 SAM 和 PAM 的一点理解
  10. CentOS6.9安装python3