首页>代码>基于SpringBoot的轻量级Java快速开发平台>/X-SpringBoot/src/main/java/com/suke/czx/common/aspect/SysLogAspect.java
package com.suke.czx.common.aspect;

import com.google.gson.Gson;
import com.suke.czx.common.annotation.SysLog;
import com.suke.czx.common.utils.HttpContextUtils;
import com.suke.czx.common.utils.IPUtils;
import com.suke.czx.modules.sys.entity.SysLogEntity;
import com.suke.czx.modules.sys.entity.SysUserEntity;
import com.suke.czx.modules.sys.service.SysLogService;
import org.apache.shiro.SecurityUtils;
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.stereotype.Component;

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


/**
 * 系统日志,切面处理类
 * 
 * @author czx
 * @email object_czx@163.com
 * @date 2017年3月8日 上午11:07:35
 */
@Aspect
@Component
public class SysLogAspect {
	@Autowired
	private SysLogService sysLogService;
	
	@Pointcut("@annotation(com.suke.czx.common.annotation.SysLog)")
	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) {
		MethodSignature signature = (MethodSignature) joinPoint.getSignature();
		Method method = signature.getMethod();

		SysLogEntity sysLog = new SysLogEntity();
		SysLog syslog = method.getAnnotation(SysLog.class);
		if(syslog != null){
			//注解上的描述
			sysLog.setOperation(syslog.value());
		}

		//请求的方法名
		String className = joinPoint.getTarget().getClass().getName();
		String methodName = signature.getName();
		sysLog.setMethod(className + "." + methodName + "()");

		//请求的参数
		Object[] args = joinPoint.getArgs();
		try{
			String params = new Gson().toJson(args[0]);
			sysLog.setParams(params);
		}catch (Exception e){

		}

		//获取request
		HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
		//设置IP地址
		sysLog.setIp(IPUtils.getIpAddr(request));

		//用户名
		String username = ((SysUserEntity) SecurityUtils.getSubject().getPrincipal()).getUsername();
		sysLog.setUsername(username);

		sysLog.setTime(time);
		sysLog.setCreateDate(new Date());
		//保存系统日志
		sysLogService.save(sysLog);
	}
}
最近下载更多
最代码-宋家辉  LV61 2022年10月15日
王泽军  LV2 2022年8月26日
taijh999  LV13 2022年6月27日
cxdxfx12  LV14 2022年6月21日
2468867327  LV10 2022年5月18日
tangjj7260  LV18 2022年4月25日
glyph  LV10 2022年3月17日
xiaolin712  LV9 2022年3月8日
美丽的巨兔12138  LV7 2022年1月27日
wanglinddad  LV54 2021年9月1日
最近浏览更多
whb5566  LV10 前天
860421  LV3 4月18日
akittyboy  LV9 4月16日
zolscy  LV12 4月9日
LHLHBBA 3月19日
暂无贡献等级
2771547747 3月6日
暂无贡献等级
ysugxx  LV9 2023年12月13日
Demo1111  LV30 2023年12月7日
lasttimeapp 2023年11月27日
暂无贡献等级
漫步的海星  LV4 2023年9月21日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友