首页>代码>基于SpringBoot+freemarker实现的人事管理系统>/person_system/person-admin/src/main/java/com/person/common/aspect/SysLogAspect.java
                
                /**
 *
 *
 *
 *
 *
 */
package com.person.common.aspect;
import com.google.gson.Gson;
import com.person.common.annotation.SysLog;
import com.person.modules.sys.entity.SysLogEntity;
import com.person.modules.sys.entity.SysUserEntity;
import com.person.modules.sys.service.SysLogService;
import com.person.common.utils.HttpContextUtils;
import com.person.common.utils.IPUtils;
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
 */
@Aspect
@Component
public class SysLogAspect {
	@Autowired
	private SysLogService sysLogService;
	
	@Pointcut("@annotation(com.person.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);
	}
}
 最近下载更多
最近下载更多
                
                ttg2025     LV3
                9月8日
            
            
        
                38735466     LV11
                8月4日
            
            
        
                gann2015     LV25
                6月6日
            
            
        
                陈小灏     LV18
                2月18日
            
            
        
                Mhgfhjyrf     LV3
                2024年12月10日
            
            
        
                15953970869     LV6
                2024年11月10日
            
            
        
                talete     LV3
                2024年10月10日
            
            
        
                微信网友_7134912998903808     LV15
                2024年8月29日
            
            
        
                255921158     LV5
                2024年7月16日
            
            
        
                2578193624     LV2
                2024年6月1日
            
            
         最近浏览更多
最近浏览更多
                
                ttg2025     LV3
                9月8日
            
            
        
                atheking     LV18
                9月4日
            
            
        
                38735466     LV11
                8月4日
            
            
        
                nanjifeng8    
                7月8日
            
            
                    暂无贡献等级
            
        
                短巷拥猫     LV12
                6月23日
            
            
        
                3334004690     LV11
                6月19日
            
            
        
                gann2015     LV25
                6月6日
            
            
        
                luomengqi    
                6月2日
            
            
                    暂无贡献等级
            
        
                yangguang     LV8
                5月1日
            
            
        
                docnnxxy688    
                3月16日
            
            
                    暂无贡献等级
            
        
 
                 
                 
                