首页>代码>SSM+apache Shiro+Jquery+Maven构建的网站后台管理系统>/CGB-JT-SYS-V1.05/src/main/java/com/jt/common/aspect/SysLogAspect.java
package com.jt.common.aspect;

import java.lang.reflect.Method;
import java.util.Date;

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 com.fasterxml.jackson.databind.ObjectMapper;
import com.jt.common.annotation.RequestLog;
import com.jt.common.exception.ServiceException;
import com.jt.common.util.IPUtils;
import com.jt.sys.dao.SysLogDao;
import com.jt.sys.entity.SysLog;

@Aspect
@Component
public class SysLogAspect {
	@Autowired
	private SysLogDao sysLogDao;
	@Pointcut("@annotation(com.jt.common.annotation.RequestLog)")
	public void logPointCut(){}
	
	@Around("logPointCut()")
	public Object around(ProceedingJoinPoint point) throws Throwable {
		System.out.println("===SyslogAspect===");
		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();
		Class<?> classTarget=joinPoint.getTarget().getClass();  
	    Class<?>[] par=((MethodSignature) joinPoint.getSignature()).getParameterTypes();  
	    Method targetMethod=null;
	    try {
			targetMethod=classTarget.getMethod(method.getName(), par);
		} catch (Exception e1) {
			e1.printStackTrace();
			throw new ServiceException(e1.getMessage());
		}
		SysLog sysLog = new SysLog();
		RequestLog requestLog = targetMethod.getAnnotation(RequestLog.class);
		System.out.println("requestLog="+requestLog);
		if(requestLog != null){
			//注解上的描述
			System.out.println("oper=="+requestLog.value());
			sysLog.setOperation(requestLog.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]);//Gson
			String params=new ObjectMapper().writeValueAsString(args[0]);//Jackson
			sysLog.setParams(params);
		}catch (Exception e){
            e.printStackTrace();
		}
		//设置IP地址
		sysLog.setIp(IPUtils.getIpAddr());
		//用户名
		String username =(String)SecurityUtils.getSubject().getPrincipal();
		sysLog.setUsername(username);
		sysLog.setTime(time);
		sysLog.setCreateDate(new Date());
		//保存系统日志
		sysLogDao.insertObject(sysLog);
	}
}
最近下载更多
hihhhh  LV6 2022年12月18日
mayya86  LV1 2022年10月2日
gggxiazai  LV3 2022年3月30日
xiaohe229  LV8 2022年1月25日
543666826  LV33 2021年11月19日
1900110734  LV6 2020年12月4日
qazzaq123  LV20 2020年11月23日
guoruchao  LV13 2020年9月28日
sccs1984  LV15 2020年9月23日
Shuxian Dong  LV8 2020年8月22日
最近浏览更多
xuweiwowzy  LV5 1月14日
gnnhka  LV10 2023年12月26日
WBelong  LV7 2023年12月26日
tmf852  LV5 2023年12月18日
dasd123 2023年11月29日
暂无贡献等级
qwerdf963  LV3 2023年11月20日
麦子8888  LV5 2023年11月13日
随手一个用户名  LV1 2023年10月25日
haotzy  LV3 2023年10月8日
空心菜4  LV9 2023年7月2日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友