com.github.javaclub.base.aop.SysLogAspect Maven / Gradle / Ivy
package com.github.javaclub.base.aop;
import java.util.Map;
import java.util.Objects;
import javax.servlet.ServletRequest;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import com.alibaba.fastjson.JSONObject;
import com.github.javaclub.base.domain.AdminUser;
import com.github.javaclub.base.domain.SysLog;
import com.github.javaclub.base.service.SysLogService;
import com.github.javaclub.sword.core.BizObjects;
import com.github.javaclub.sword.core.Maps;
import com.github.javaclub.sword.core.Numbers;
import com.github.javaclub.sword.util.JSONStringUtil;
import com.github.javaclub.sword.util.javabean.JavaTypeHandlers;
import com.github.javaclub.sword.web.holder.AdminUserHolder;
import com.github.javaclub.toolbox.ToolBox.Strings;
import com.github.javaclub.toolbox.ToolBox.Web;
import com.github.javaclub.toolbox.enumtype.ActionType;
import lombok.AllArgsConstructor;
@Aspect
@Component
@AllArgsConstructor
public class SysLogAspect {
private final SysLogService sysLogService;
@Around("@annotation(sysLog)")
public Object around(ProceedingJoinPoint joinPoint, com.github.javaclub.base.annotation.SysLog sysLog) throws Throwable {
long beginTime = System.currentTimeMillis();
//执行方法
Object result = joinPoint.proceed();
//执行时长(毫秒)
long time = System.currentTimeMillis() - beginTime;
AdminUser user = (AdminUser) AdminUserHolder.get();
if (null != user && !Numbers.isPositiveNumber(user.getId())) {
return result;
}
SysLog logEntity = new SysLog();
if (sysLog != null) {
//注解上的描述
logEntity.setOperation(sysLog.value());
logEntity.setActionType(sysLog.actionType().getType());
}
//请求的方法名
String shortlyName = BizObjects.getShortlyFullName(joinPoint.getTarget().getClass());
String methodName = joinPoint.getSignature().getName();
logEntity.setMethod(shortlyName + "." + methodName + "()");
//设置IP地址
String ip = Web.requestClientIpAddr();
logEntity.setIp(ip);
// 用户名
logEntity.setUsername(null == user ? Strings.NA : user.getUsername());
logEntity.setUserId(null == user ? null : user.getId());
// 请求的参数
Object[] args = joinPoint.getArgs();
if (BizObjects.isNotEmpty(args)) {
Object params = this.filter(args[0], logEntity);
if (null != params && params instanceof JSONObject) {
if (Strings.equals("super", ((JSONObject) params).getString("account"))) {
return result;
}
}
logEntity.setParams(JSONObject.toJSONString(params));
}
logEntity.setTime(time);
//保存系统日志
sysLogService.save(logEntity);
return result;
}
Object filter(Object param, SysLog logEntity) {
if (null == param || JavaTypeHandlers.supports(param.getClass())) {
return Objects.toString(param, Strings.EMPTY);
}
if (param instanceof ServletRequest || param instanceof ServletRequest
|| param instanceof MultipartFile) {
return param.getClass().getName();
}
try {
JSONObject obj = JSONStringUtil.toJsonObject(param);
if (null == obj) {
return Strings.EMPTY;
}
Map copy = Maps.mask(obj);
if (null != copy) {
if (null != obj.getString("account") &&
Objects.equals(ActionType.LOGIN.getType(), logEntity.getActionType())) {
logEntity.setUsername(obj.getString("account"));
}
return copy;
}
} catch (Throwable e) {
}
return param;
}
}