RequestLogAspect.java 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. package jnpf.aop;
  2. import cn.hutool.http.useragent.UserAgent;
  3. import cn.hutool.http.useragent.UserAgentUtil;
  4. import jnpf.base.LogSortEnum;
  5. import jnpf.base.UserInfo;
  6. import jnpf.config.ConfigValueUtil;
  7. import jnpf.entity.LogEntity;
  8. import jnpf.service.LogService;
  9. import jnpf.util.*;
  10. import lombok.extern.slf4j.Slf4j;
  11. import org.aspectj.lang.ProceedingJoinPoint;
  12. import org.aspectj.lang.Signature;
  13. import org.aspectj.lang.annotation.Around;
  14. import org.aspectj.lang.annotation.Aspect;
  15. import org.aspectj.lang.annotation.Pointcut;
  16. import org.aspectj.lang.reflect.MethodSignature;
  17. import org.springframework.beans.factory.annotation.Autowired;
  18. import org.springframework.core.annotation.Order;
  19. import org.springframework.stereotype.Component;
  20. import jnpf.annotation.HandleLog;
  21. import org.springframework.web.multipart.MultipartFile;
  22. import java.lang.reflect.Method;
  23. import java.util.Date;
  24. /**
  25. * @author JNPF开发平台组
  26. * @version V3.1.0
  27. * @copyright 引迈信息技术有限公司
  28. * @date 2021/3/15 17:12
  29. */
  30. @Slf4j
  31. @Aspect
  32. @Component
  33. @Order(2)
  34. public class RequestLogAspect {
  35. @Autowired
  36. private ConfigValueUtil configValueUtil;
  37. @Autowired
  38. private LogService logService;
  39. @Pointcut("within(jnpf.*.controller.* || jnpf.controller.*)")// && !within(jnpf.controller.UtilsController)
  40. public void requestLog() {
  41. }
  42. @Around("requestLog()")
  43. public Object doAroundService(ProceedingJoinPoint pjp) throws Throwable {
  44. long startTime = System.currentTimeMillis();
  45. Object obj = pjp.proceed();
  46. if(ServletUtil.getRequest() == null){
  47. return obj;
  48. }
  49. long costTime = System.currentTimeMillis() - startTime;
  50. UserInfo userInfo = UserProvider.getUser();
  51. if(userInfo.getUserId() != null && (!configValueUtil.isMultiTenancy() || TenantHolder.getLocalTenantCache() != null)) {
  52. // 得到请求参数
  53. Object[] args = pjp.getArgs();
  54. Signature signature = pjp.getSignature();
  55. printLog(userInfo, costTime, obj, args, signature);
  56. try {
  57. // 判断是否需要操作日志
  58. MethodSignature methodSignature = (MethodSignature) pjp.getSignature();
  59. // 得到请求方法
  60. Method method = methodSignature.getMethod();
  61. HandleLog methodAnnotation = method.getAnnotation(HandleLog.class);
  62. if (methodAnnotation != null) {
  63. String moduleName = methodAnnotation.moduleName();
  64. String requestMethod = methodAnnotation.requestMethod();
  65. handleLog(userInfo, costTime, obj, moduleName, requestMethod, args, signature);
  66. }
  67. } catch (Exception e) {
  68. log.error("记录操作日志发生错误:" + e.getMessage());
  69. }
  70. }
  71. return obj;
  72. }
  73. /**
  74. * 请求日志
  75. *
  76. * @param userInfo
  77. * @param costTime
  78. */
  79. private void printLog(UserInfo userInfo, long costTime, Object obj, Object[] args, Signature signature) {
  80. LogEntity entity = new LogEntity();
  81. entity.setId(RandomUtil.uuId());
  82. entity.setType(LogSortEnum.Request.getCode());
  83. entity.setUserId(userInfo.getUserId());
  84. entity.setUserName(userInfo.getUserName() + "/" + userInfo.getUserAccount());
  85. //请求耗时
  86. entity.setRequestDuration((int) costTime);
  87. entity.setRequestUrl(ServletUtil.getRequest().getServletPath());
  88. entity.setRequestMethod(ServletUtil.getRequest().getMethod());
  89. String ipAddr = IpUtil.getIpAddr();
  90. entity.setIpAddress(ipAddr);
  91. entity.setIpAddressName(IpUtil.getIpCity(ipAddr));
  92. entity.setCreatorTime(new Date());
  93. UserAgent userAgent = UserAgentUtil.parse(ServletUtil.getUserAgent());
  94. if (userAgent != null) {
  95. entity.setPlatForm(userAgent.getPlatform().getName() + " " + userAgent.getOsVersion());
  96. entity.setBrowser(userAgent.getBrowser().getName() + " " + userAgent.getVersion());
  97. }
  98. String declaringTypeName = signature.getDeclaringTypeName();
  99. String name = signature.getName();
  100. entity.setRequestTarget(declaringTypeName + "." + name);
  101. entity.setJsons(obj + "");
  102. StringBuilder stringBuilder = new StringBuilder();
  103. for (Object o : args) {
  104. // 如果是MultipartFile则为导入
  105. if (o instanceof MultipartFile) {
  106. stringBuilder.append("{\"originalFilename\":\"" + ((MultipartFile) o).getOriginalFilename() + "\",");
  107. stringBuilder.append("\"contentType\":\"" + ((MultipartFile) o).getContentType() + "\",");
  108. stringBuilder.append("\"name\":\"" + ((MultipartFile) o).getName() + "\",");
  109. stringBuilder.append("\"resource\":\"" + ((MultipartFile) o).getResource() + "\",");
  110. stringBuilder.append("\"size\":\"" + ((MultipartFile) o).getSize() + "\"}");
  111. }
  112. }
  113. if (stringBuilder.length() > 0) {
  114. entity.setRequestParam(stringBuilder.toString());
  115. } else {
  116. entity.setRequestParam(JsonUtil.getObjectToString(args));
  117. }
  118. ThreadPoolExecutorUtil.getExecutor().execute(()->{
  119. logService.save(entity);
  120. });
  121. }
  122. /**
  123. * 添加操作日志
  124. *
  125. * @param userInfo 用户信息
  126. * @param costTime 操作耗时
  127. * @param obj 请求结果
  128. * @param moduleName 模块名称
  129. * @param requestMethod 请求方法
  130. * @param args 请求参数
  131. */
  132. private void handleLog(UserInfo userInfo, long costTime, Object obj, String moduleName, String requestMethod, Object[] args, Signature signature) {
  133. LogEntity entity = new LogEntity();
  134. entity.setId(RandomUtil.uuId());
  135. entity.setType(LogSortEnum.Operate.getCode());
  136. entity.setUserId(userInfo.getUserId());
  137. entity.setUserName(userInfo.getUserName() + "/" + userInfo.getUserAccount());
  138. //请求耗时
  139. entity.setRequestDuration((int) costTime);
  140. entity.setRequestMethod(ServletUtil.getRequest().getMethod());
  141. entity.setRequestUrl(ServletUtil.getRequest().getServletPath());
  142. String ipAddr = IpUtil.getIpAddr();
  143. entity.setIpAddress(ipAddr);
  144. entity.setIpAddressName(IpUtil.getIpCity(ipAddr));
  145. entity.setCreatorTime(new Date());
  146. // 请求设备
  147. UserAgent userAgent = UserAgentUtil.parse(ServletUtil.getUserAgent());
  148. if (userAgent != null) {
  149. entity.setPlatForm(userAgent.getPlatform().getName() + " " + userAgent.getOsVersion());
  150. entity.setBrowser(userAgent.getBrowser().getName() + " " + userAgent.getVersion());
  151. }
  152. // 操作模块
  153. entity.setModuleName(moduleName);
  154. String declaringTypeName = signature.getDeclaringTypeName();
  155. String name = signature.getName();
  156. entity.setRequestTarget(declaringTypeName + "." + name);
  157. // 操作记录
  158. StringBuilder stringBuilder = new StringBuilder();
  159. for (Object o : args) {
  160. // 如果是MultipartFile则为导入
  161. if (o instanceof MultipartFile) {
  162. stringBuilder.append("{\"originalFilename\":\"" + ((MultipartFile) o).getOriginalFilename() + "\",");
  163. stringBuilder.append("\"contentType\":\"" + ((MultipartFile) o).getContentType() + "\",");
  164. stringBuilder.append("\"name\":\"" + ((MultipartFile) o).getName() + "\",");
  165. stringBuilder.append("\"resource\":\"" + ((MultipartFile) o).getResource() + "\",");
  166. stringBuilder.append("\"size\":\"" + ((MultipartFile) o).getSize() + "\"}");
  167. }
  168. }
  169. if (stringBuilder.length() > 0) {
  170. entity.setRequestParam(stringBuilder.toString());
  171. } else {
  172. entity.setRequestParam(JsonUtil.getObjectToString(args));
  173. }
  174. entity.setJsons(obj + "");
  175. ThreadPoolExecutorUtil.getExecutor().execute(()->{
  176. logService.save(entity);
  177. });
  178. }
  179. /// 后面可能会用
  180. // /**
  181. // * 判断是否为导入导出
  182. // *
  183. // * @return
  184. // */
  185. // private String getRequestMethod() {
  186. // //得到请求方式
  187. // String methodType = ServletUtil.getRequest().getMethod();
  188. // // 得到当前请求的尾缀
  189. // String endWith = null;
  190. // String servletPath = ServletUtil.getServletPath();
  191. // if (StringUtil.isNotEmpty(servletPath)) {
  192. // String[] path = servletPath.split("/");
  193. // int length = path.length;
  194. // if (length > 5) {
  195. // endWith = path[length - 2] + "/" + path[length - 1];
  196. // }
  197. // }
  198. // // 如果是GET请求且请求后缀是'/Action/Export'则判定为导出
  199. // if (HandleMethodEnum.GET.getRequestType().equals(methodType)) {
  200. // methodType = "Action/Export".equals(endWith) ? "EXPORT" : "GET";
  201. // } else if (HandleMethodEnum.POST.getRequestType().equals(methodType)) {
  202. // methodType = "Action/Import".equals(endWith) ? "IMPORT" : "GET";
  203. // }
  204. // return methodType;
  205. // }
  206. // /**
  207. // * 判断是否为导入导出
  208. // *
  209. // * @return
  210. // */
  211. // private String getRequestModuleName() {
  212. // //得到Url
  213. // String requestURI = ServletUtil.getRequest().getRequestURI();
  214. // // 取模块名
  215. // if (StringUtil.isNotEmpty(requestURI)) {
  216. // String[] split = requestURI.split("/");
  217. // if (split.length > 2) {
  218. // String url = split[1];
  219. // // 得到所在模块
  220. // String moduleName = HandleModuleEnum.getModuleByURL(url);
  221. // return moduleName;
  222. // }
  223. // }
  224. // return "";
  225. // }
  226. ///
  227. }