You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

UserLogAspect.java 7.6 kB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package com.stonedt.intelligence.aspect;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.stonedt.intelligence.entity.User;
  5. import com.stonedt.intelligence.service.UserLogService;
  6. import com.stonedt.intelligence.util.*;
  7. import org.aspectj.lang.ProceedingJoinPoint;
  8. import org.aspectj.lang.annotation.Around;
  9. import org.aspectj.lang.annotation.Aspect;
  10. import org.aspectj.lang.annotation.Pointcut;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.beans.factory.annotation.Value;
  13. import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
  14. import org.springframework.stereotype.Component;
  15. import org.aspectj.lang.reflect.MethodSignature;
  16. import javax.servlet.http.HttpServletRequest;
  17. import java.lang.reflect.Method;
  18. import java.util.HashMap;
  19. import java.util.Map;
  20. /**
  21. * description: 用户操作日志 <br>
  22. * date: 2020/5/9 12:31 <br>
  23. * author: huajiancheng <br>
  24. * version: 1.0 <br>
  25. */
  26. @Aspect
  27. @Component
  28. public class UserLogAspect {
  29. @Autowired
  30. UserLogService userLogService;
  31. @Autowired
  32. UserUtil userUtil;
  33. @Value("${kafuka.url}")
  34. private String kafuka_url;
  35. // 切点
  36. @Pointcut("@annotation(com.stonedt.intelligence.aspect.SystemControllerLog)")
  37. public void controllerAspect() {
  38. }
  39. @Around("controllerAspect()")
  40. public Object around(ProceedingJoinPoint point) {
  41. Object result = null;
  42. long times = System.currentTimeMillis();
  43. try {
  44. // 执行方法
  45. result = point.proceed();
  46. } catch (Throwable e) {
  47. e.printStackTrace();
  48. }
  49. long timee = System.currentTimeMillis();
  50. // 保存日志
  51. saveLog(point, times, timee);
  52. return result;
  53. }
  54. /**
  55. * @param joinPoint JoinPoint 对象
  56. * @param times 开始时间
  57. * @param timee 结束时间
  58. * @return void
  59. * @description: 保存用户的操作日志 <br>
  60. * @version: 1.0 <br>
  61. * @date: 2020/5/9 17:41 <br>
  62. * @author: huajiancheng <br>
  63. */
  64. private void saveLog(ProceedingJoinPoint joinPoint, long timestamps, long timestampe) {
  65. JSONObject responseJson = new JSONObject();
  66. try {
  67. MethodSignature signature = (MethodSignature) joinPoint.getSignature();
  68. Method method = signature.getMethod();
  69. SystemControllerLog logAnnotation = method.getAnnotation(SystemControllerLog.class);
  70. String submodule = "";
  71. String operation = "";
  72. if (logAnnotation != null) {
  73. // 注解上的描述
  74. submodule = logAnnotation.submodule();
  75. operation = logAnnotation.operation();
  76. }
  77. // 请求的方法名
  78. String class_name = joinPoint.getTarget().getClass().getName();
  79. String method_name = signature.getName();
  80. // 请求的方法参数值
  81. Object[] args = joinPoint.getArgs();
  82. // 请求的方法参数名称
  83. LocalVariableTableParameterNameDiscoverer tableParameterNameDiscoverer = new LocalVariableTableParameterNameDiscoverer();
  84. String[] paramNames = tableParameterNameDiscoverer.getParameterNames(method);
  85. JSONArray paramsArray = new JSONArray(); // 参数
  86. if (args != null && paramNames != null) {
  87. for (int i = 0; i < args.length; i++) {
  88. JSONObject paramsJson = new JSONObject();
  89. String paramKey = paramNames[i];
  90. if (!paramKey.equals("mv") && !paramKey.equals("request") && !paramKey.equals("session")) {
  91. String paramValue = String.valueOf(args[i]);
  92. if (!paramValue.equals("null")) {
  93. paramsJson.put(paramKey, paramValue);
  94. paramsArray.add(paramsJson);
  95. }
  96. }
  97. }
  98. }
  99. // 获取request
  100. HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
  101. User user = userUtil.getuser(request);
  102. Long user_id = user.getUser_id();
  103. String organization_id = user.getOrganization_id();
  104. Integer status = user.getStatus();
  105. String username = user.getUsername();
  106. String create_time = DateUtil.getNowTime();
  107. Map<String, Object> organizeMap = new HashMap<String, Object>();
  108. organizeMap.put("organization_id", organization_id);
  109. Map<String, Object> organizeResponseMap = userLogService.getUserOrganizationById(organizeMap);
  110. String organization_name = String.valueOf(organizeResponseMap.get("organization_name"));
  111. String term_of_validity = String.valueOf(organizeResponseMap.get("term_of_validity"));
  112. term_of_validity = term_of_validity.substring(0,19);
  113. String article_public_id = MD5Util.getMD5(user_id + create_time + method_name + method_name + paramsArray.toJSONString());
  114. String times = DateUtil.stampToDate(String.valueOf(timestamps));
  115. String timee = DateUtil.stampToDate(String.valueOf(timestampe));
  116. // Map<String, Object> moudleMap = userLogService.getMoudleByName(module_name);
  117. Map<String, Object> moudleMap = userLogService.getSubMoudleByName(submodule);
  118. String module_name = String.valueOf(moudleMap.get("module_name"));
  119. String submodule_name = String.valueOf(moudleMap.get("submodule_name"));
  120. String submodule_id = String.valueOf(moudleMap.get("submodule_id"));
  121. String module_id = String.valueOf(moudleMap.get("module_id"));
  122. responseJson.put("user_id", user_id);
  123. responseJson.put("article_public_id", article_public_id);
  124. responseJson.put("organization_id", organization_id);
  125. responseJson.put("create_time", create_time);
  126. responseJson.put("username", username);
  127. responseJson.put("status", status);
  128. responseJson.put("parameters", paramsArray.toJSONString());
  129. responseJson.put("organization_name", organization_name);
  130. responseJson.put("class_name", class_name);
  131. responseJson.put("method_name", method_name);
  132. responseJson.put("times", times);
  133. responseJson.put("timee", timee);
  134. responseJson.put("module_id", module_id);
  135. responseJson.put("module_name", module_name);
  136. responseJson.put("operation", operation);
  137. responseJson.put("submodule_id", submodule_id);
  138. responseJson.put("submodule_name", submodule_name);
  139. responseJson.put("term_of_validity", term_of_validity);
  140. responseJson.put("es_index", "stonedt_portaluserlog");
  141. responseJson.put("es_type", "infor");
  142. responseJson.put("hbase_table", "stonedt_portaluserlog");
  143. String result = MyHttpRequestUtil.doPostKafka("proStonedtData", responseJson.toJSONString(), kafuka_url);
  144. if (result.equals("200")) {
  145. System.out.println("发送成功!");
  146. }
  147. // SysLog sysLog = JSON.toJavaObject(responseJson, SysLog.class);
  148. // 保存系统日志
  149. // userLogService.saveUserLog(sysLog);
  150. } catch (Exception e) {
  151. e.getMessage();
  152. }
  153. }
  154. }

一款开源免费的舆情系统。 支持本地化部署,支持在线体验。 支持对海量舆情数据分析和挖掘。