package com.platform.common.aspect;@b@@b@import com.alibaba.fastjson.JSONObject;@b@import com.platform.common.annotation.SysLog;@b@import com.platform.common.utils.Constant;@b@import com.platform.common.utils.HttpContextUtils;@b@import com.platform.common.utils.IpUtils;@b@import com.platform.modules.sys.entity.SysLogEntity;@b@import com.platform.modules.sys.entity.SysUserEntity;@b@import com.platform.modules.sys.service.SysLogService;@b@import lombok.RequiredArgsConstructor;@b@import org.apache.shiro.SecurityUtils;@b@import org.aspectj.lang.ProceedingJoinPoint;@b@import org.aspectj.lang.annotation.Around;@b@import org.aspectj.lang.annotation.Aspect;@b@import org.aspectj.lang.annotation.Pointcut;@b@import org.aspectj.lang.reflect.MethodSignature;@b@import org.springframework.stereotype.Component;@b@@b@import javax.servlet.http.HttpServletRequest;@b@import java.lang.reflect.Method;@b@@b@/**@b@ * 系统日志,切面处理类@b@ *@b@ * @author 李鹏军@b@ */@b@@RequiredArgsConstructor@b@@Aspect@b@@Component@b@public class SysLogAspect {@b@ private final SysLogService sysLogService;@b@@b@ @Pointcut("@annotation(com.platform.common.annotation.SysLog)")@b@ public void logPointCut() {@b@@b@ }@b@@b@ @Around("logPointCut()")@b@ public Object around(ProceedingJoinPoint point) throws Throwable {@b@ long beginTime = System.currentTimeMillis();@b@ //执行方法@b@ Object result = point.proceed();@b@ //执行时长(毫秒)@b@ long time = System.currentTimeMillis() - beginTime;@b@@b@ //保存日志@b@ saveSysLog(point, time);@b@@b@ return result;@b@ }@b@@b@ private void saveSysLog(ProceedingJoinPoint joinPoint, long time) {@b@ MethodSignature signature = (MethodSignature) joinPoint.getSignature();@b@ Method method = signature.getMethod();@b@@b@ SysLogEntity sysLog = new SysLogEntity();@b@ SysLog syslog = method.getAnnotation(SysLog.class);@b@ if (syslog != null) {@b@ //注解上的描述@b@ sysLog.setOperation(syslog.value());@b@ }@b@@b@ //请求的方法名@b@ String className = joinPoint.getTarget().getClass().getName();@b@ String methodName = signature.getName();@b@ sysLog.setMethod(className + Constant.DOT + methodName + "()");@b@@b@ //请求的参数@b@ Object[] args = joinPoint.getArgs();@b@ try {@b@ String params = JSONObject.toJSONString(args);@b@ sysLog.setParams(params);@b@@b@ //获取request@b@ HttpServletRequest request = HttpContextUtils.getHttpServletRequest();@b@ //设置IP地址@b@ sysLog.setIp(IpUtils.getIpAddr(request));@b@@b@ //用户名@b@ String userName = ((SysUserEntity) SecurityUtils.getSubject().getPrincipal()).getUserName();@b@ sysLog.setUserName(userName);@b@@b@ sysLog.setTime(time);@b@ //保存系统日志@b@ sysLogService.save(sysLog);@b@ } catch (Exception ignored) {@b@@b@ }@b@ }@b@}
package com.platform.common.annotation;@b@@b@import java.lang.annotation.*;@b@@b@/**@b@ * 系统日志注解@b@ *@b@ * @author 李鹏军@b@ */@b@@Target(ElementType.METHOD)@b@@Retention(RetentionPolicy.RUNTIME)@b@@Documented@b@public @interface SysLog {@b@@b@ String value() default "操作日志";@b@}