中央热水项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
3.6 KiB

package com.mh.user.aspect;
import javax.servlet.http.HttpServletRequest;
import com.alibaba.fastjson2.JSONObject;
import com.mh.user.annotation.SysLogger;
import com.mh.user.model.SysLog;
import com.mh.user.service.SysLogService;
import com.mh.user.utils.HttpUtils;
import com.mh.user.utils.IPUtils;
import com.mh.user.utils.SecurityUtils;
import org.aspectj.lang.JoinPoint;
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.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
import java.util.Date;
/**
* 系统日志,切面处理类,记录日志
*/
@Aspect
@Component
@Lazy(value = false)
public class SysLogAspect {
@Autowired
private SysLogService sysLogService;
// @Pointcut("execution(* com.mh.*.service.*.*(..))")
@Pointcut("@annotation(com.mh.user.annotation.SysLogger)")
public void logPointCut() {
}
@Around("logPointCut()")
public Object around(ProceedingJoinPoint point) throws Throwable {
long beginTime = System.currentTimeMillis();
// 执行方法
Object result = point.proceed();
// 执行时长(毫秒)
long time = System.currentTimeMillis() - beginTime;
// 保存日志
saveSysLog(point, time);
return result;
}
private void saveSysLog(ProceedingJoinPoint joinPoint, long time) {
SysLog sysLog = new SysLog();
String methodName = joinPoint.getSignature().getName();
Method method = currentMethod(joinPoint,methodName);
SysLogger sysLogger = method.getAnnotation(SysLogger.class);
String module = sysLogger.title(); //操作模块
String optDesc=sysLogger.optDesc();//业务操作描述
sysLog.setOptDesc(optDesc);
sysLog.setOperation(module);
String userName = SecurityUtils.getUsername(); //用户名称
if(joinPoint.getTarget() instanceof SysLogService) {
return ;
}
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
// 请求的方法名
String className = joinPoint.getTarget().getClass().getName();
String methodName1 = signature.getName();
sysLog.setMethod(className + "." + methodName1 + "()");
// 请求的参数
Object[] args = joinPoint.getArgs();
try{
String params = JSONObject.toJSONString(args[0]);
if(params.length() > 200) {
params = params.substring(0, 200) + "...";
}
sysLog.setParams(params);
} catch (Exception e){
}
// 获取request
HttpServletRequest request = HttpUtils.getHttpServletRequest();
// 设置IP地址
sysLog.setIp(IPUtils.getIpAddr(request));
String ip=IPUtils.getRemoteIp();
// 用户名
sysLog.setUserName(userName);
Date date1 = SecurityUtils.getLoginTime();
// 登录时间
sysLog.setCreateTime(date1);
Date date=new Date();
// 最后操作时间
sysLog.setLastUpdateTime(date);
// 执行时长(毫秒)
sysLog.setTime(time);
// 保存系统日志
sysLogService.save(sysLog);
}
/**
* 获取当前执行的方法
*
* @param joinPoint 连接点
* @param methodName 方法名称
* @return 方法
*/
private Method currentMethod(JoinPoint joinPoint, String methodName) {
/**
* 获取目标类的所有方法,找到当前要执行的方法
*/
Method[] methods = joinPoint.getTarget().getClass().getMethods();
Method resultMethod = null;
for (Method method : methods) {
if (method.getName().equals(methodName)) {
resultMethod = method;
break;
}
}
return resultMethod;
}
}