package com.common.aspect; import com.alibaba.fastjson.JSONObject; import com.common.security.utils.IpUtil; import com.deloitte.system.model.SysLog; import eu.bitwalker.useragentutils.UserAgent; import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; 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; /** * @author 廖振钦 * @date 2022-02-17 * @company deloitte */ @Slf4j @Aspect @Component @Order(1) public class LogReturnAspect { @AfterReturning(pointcut = "execution(* com..controller..*.*(..))" + "&& (@annotation(org.springframework.web.bind.annotation.RequestMapping)" + "|| @annotation(org.springframework.web.bind.annotation.GetMapping)" + "|| @annotation(org.springframework.web.bind.annotation.PostMapping)" + "|| @annotation(org.springframework.web.bind.annotation.DeleteMapping)" + "|| @annotation(org.springframework.web.bind.annotation.PatchMapping))"+ "&& !@annotation(com.common.annotation.NoLog)", returning = "objectReturn") public void doAfterCalssReturning(Object objectReturn) throws Throwable { try { handleLog(objectReturn); } catch (Throwable e) { log.error("LogReturnAspect>>>>>>>>", e); throw e; } } protected void handleLog(Object objectReturn) { HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest(); String uri = request.getRequestURI(); String ip = IpUtil.getIpAddr(request); // log.info(Thread.currentThread().getName()+",returnContent=" + JSONObject.toJSONString(objectReturn)); SysLog sysLog = new SysLog(); if (null != objectReturn) { sysLog.ip(ip).retContent("response:"+ JSONObject.toJSONString(objectReturn)).uri(uri); sysLog.save(); } } }