package org.jeecg.modules.arj.aop; import com.alibaba.fastjson.JSONObject; import org.apache.shiro.SecurityUtils; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.*; import org.jeecg.common.system.vo.LoginUser; import org.jeecg.common.util.IpUtils; import org.jeecg.common.util.SpringContextUtils; import org.jeecg.modules.system.entity.SysLog; import org.jeecg.modules.system.service.ISysLogService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.http.HttpServletRequest; import java.util.Arrays; import java.util.Date; /** * 切面日志的处理 * zk */ @Aspect @Component @Order(1) public class WebLogAspect { private static Logger logger = LoggerFactory.getLogger(WebLogAspect.class); @Autowired private ISysLogService sysLogService; /** * 定义切入点,切入点为com.example.aop下的所有函数 */ @Pointcut("execution(public * org.jeecg.modules.arj.controller..*.*(..))") public void webLog() { } /** * 前置通知:在连接点之前执行的通知 */ @Before("webLog()") public void doBefore(JoinPoint joinPoint) throws Exception { // 接收到请求,记录请求内容 ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); //打印请求内容 logger.info("请求内容开始 请求地址:{} 请求方式:{}", request.getRequestURL().toString(), request.getMethod()); logger.info("请求类方法: {} 请求类方法参数: {} ", joinPoint.getSignature(), Arrays.toString(joinPoint.getArgs())); SysLog syslog = new SysLog(); //获取request request = SpringContextUtils.getHttpServletRequest(); //请求的参数 //获取登录用户信息 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal(); syslog.setCreateTime( new Date()); syslog.setUsername(sysUser.getUsername()); syslog.setUserid(sysUser.getUserIdentity().toString()); // syslog.setIp(org.jeecg.modules.arj.util.IpUtils.getIpAddr(request)); syslog.setLogContent(request.getRequestURL().toString()); if( request.getRequestURL().toString() != null && request.getRequestURL().toString().indexOf("edit") > -1){ syslog.setRequestParam(JSONObject.toJSONString(joinPoint.getArgs())); }else syslog.setRequestParam(JSONObject.toJSONString(joinPoint.getArgs())); sysLogService.save(syslog); } @After("webLog()") public void doAfter(JoinPoint joinPoint) throws Exception { } @AfterReturning(returning = "obj", pointcut = "webLog()") public void doAfterReturning(Object obj) throws Throwable { } }