c7bbd0acf79afcfd32bfc02b2b8d7ee50f6e1c68.svn-base 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. package org.jeecg.common.aspect;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.alibaba.fastjson.serializer.PropertyFilter;
  4. import org.apache.shiro.SecurityUtils;
  5. import org.aspectj.lang.JoinPoint;
  6. import org.aspectj.lang.ProceedingJoinPoint;
  7. import org.aspectj.lang.annotation.Around;
  8. import org.aspectj.lang.annotation.Aspect;
  9. import org.aspectj.lang.annotation.Pointcut;
  10. import org.aspectj.lang.reflect.MethodSignature;
  11. import org.jeecg.common.api.dto.LogDTO;
  12. import org.jeecg.common.api.vo.Result;
  13. import org.jeecg.common.aspect.annotation.AutoLog;
  14. import org.jeecg.common.constant.CommonConstant;
  15. import org.jeecg.common.constant.enums.ModuleType;
  16. import org.jeecg.modules.base.service.BaseCommonService;
  17. import org.jeecg.common.system.vo.LoginUser;
  18. import org.jeecg.common.util.IPUtils;
  19. import org.jeecg.common.util.SpringContextUtils;
  20. import org.jeecg.common.util.oConvertUtils;
  21. import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
  22. import org.springframework.stereotype.Component;
  23. import org.springframework.validation.BindingResult;
  24. import org.springframework.web.multipart.MultipartFile;
  25. import javax.annotation.Resource;
  26. import javax.servlet.ServletRequest;
  27. import javax.servlet.ServletResponse;
  28. import javax.servlet.http.HttpServletRequest;
  29. import java.lang.reflect.Method;
  30. import java.util.Date;
  31. /**
  32. * 系统日志,切面处理类
  33. *
  34. * @Author scott
  35. * @email jeecgos@163.com
  36. * @Date 2018年1月14日
  37. */
  38. @Aspect
  39. @Component
  40. public class AutoLogAspect {
  41. @Resource
  42. private BaseCommonService baseCommonService;
  43. @Pointcut("@annotation(org.jeecg.common.aspect.annotation.AutoLog)")
  44. public void logPointCut() {
  45. }
  46. @Around("logPointCut()")
  47. public Object around(ProceedingJoinPoint point) throws Throwable {
  48. long beginTime = System.currentTimeMillis();
  49. //执行方法
  50. Object result = point.proceed();
  51. //执行时长(毫秒)
  52. long time = System.currentTimeMillis() - beginTime;
  53. //保存日志
  54. saveSysLog(point, time, result);
  55. return result;
  56. }
  57. private void saveSysLog(ProceedingJoinPoint joinPoint, long time, Object obj) {
  58. MethodSignature signature = (MethodSignature) joinPoint.getSignature();
  59. Method method = signature.getMethod();
  60. LogDTO dto = new LogDTO();
  61. AutoLog syslog = method.getAnnotation(AutoLog.class);
  62. if(syslog != null){
  63. //update-begin-author:taoyan date:
  64. String content = syslog.value();
  65. if(syslog.module()== ModuleType.ONLINE){
  66. content = getOnlineLogContent(obj, content);
  67. }
  68. //注解上的描述,操作日志内容
  69. dto.setLogType(syslog.logType());
  70. dto.setLogContent(content);
  71. }
  72. //请求的方法名
  73. String className = joinPoint.getTarget().getClass().getName();
  74. String methodName = signature.getName();
  75. dto.setMethod(className + "." + methodName + "()");
  76. //设置操作类型
  77. if (dto.getLogType() == CommonConstant.LOG_TYPE_2) {
  78. dto.setOperateType(getOperateType(methodName, syslog.operateType()));
  79. }
  80. //获取request
  81. HttpServletRequest request = SpringContextUtils.getHttpServletRequest();
  82. //请求的参数
  83. dto.setRequestParam(getReqestParams(request,joinPoint));
  84. //设置IP地址
  85. dto.setIp(IPUtils.getIpAddr(request));
  86. //获取登录用户信息
  87. LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
  88. if(sysUser!=null){
  89. dto.setUserid(sysUser.getUsername());
  90. dto.setUsername(sysUser.getRealname());
  91. }
  92. //耗时
  93. dto.setCostTime(time);
  94. dto.setCreateTime(new Date());
  95. //保存系统日志
  96. baseCommonService.addLog(dto);
  97. }
  98. /**
  99. * 获取操作类型
  100. */
  101. private int getOperateType(String methodName,int operateType) {
  102. if (operateType > 0) {
  103. return operateType;
  104. }
  105. if (methodName.startsWith("list")) {
  106. return CommonConstant.OPERATE_TYPE_1;
  107. }
  108. if (methodName.startsWith("add")) {
  109. return CommonConstant.OPERATE_TYPE_2;
  110. }
  111. if (methodName.startsWith("edit")) {
  112. return CommonConstant.OPERATE_TYPE_3;
  113. }
  114. if (methodName.startsWith("delete")) {
  115. return CommonConstant.OPERATE_TYPE_4;
  116. }
  117. if (methodName.startsWith("import")) {
  118. return CommonConstant.OPERATE_TYPE_5;
  119. }
  120. if (methodName.startsWith("export")) {
  121. return CommonConstant.OPERATE_TYPE_6;
  122. }
  123. return CommonConstant.OPERATE_TYPE_1;
  124. }
  125. /**
  126. * @Description: 获取请求参数
  127. * @author: scott
  128. * @date: 2020/4/16 0:10
  129. * @param request: request
  130. * @param joinPoint: joinPoint
  131. * @Return: java.lang.String
  132. */
  133. private String getReqestParams(HttpServletRequest request, JoinPoint joinPoint) {
  134. String httpMethod = request.getMethod();
  135. String params = "";
  136. if ("POST".equals(httpMethod) || "PUT".equals(httpMethod) || "PATCH".equals(httpMethod)) {
  137. Object[] paramsArray = joinPoint.getArgs();
  138. // java.lang.IllegalStateException: It is illegal to call this method if the current request is not in asynchronous mode (i.e. isAsyncStarted() returns false)
  139. // https://my.oschina.net/mengzhang6/blog/2395893
  140. Object[] arguments = new Object[paramsArray.length];
  141. for (int i = 0; i < paramsArray.length; i++) {
  142. if (paramsArray[i] instanceof BindingResult || paramsArray[i] instanceof ServletRequest || paramsArray[i] instanceof ServletResponse || paramsArray[i] instanceof MultipartFile) {
  143. //ServletRequest不能序列化,从入参里排除,否则报异常:java.lang.IllegalStateException: It is illegal to call this method if the current request is not in asynchronous mode (i.e. isAsyncStarted() returns false)
  144. //ServletResponse不能序列化 从入参里排除,否则报异常:java.lang.IllegalStateException: getOutputStream() has already been called for this response
  145. continue;
  146. }
  147. arguments[i] = paramsArray[i];
  148. }
  149. //update-begin-author:taoyan date:20200724 for:日志数据太长的直接过滤掉
  150. PropertyFilter profilter = new PropertyFilter() {
  151. @Override
  152. public boolean apply(Object o, String name, Object value) {
  153. if(value!=null && value.toString().length()>500){
  154. return false;
  155. }
  156. return true;
  157. }
  158. };
  159. params = JSONObject.toJSONString(arguments, profilter);
  160. //update-end-author:taoyan date:20200724 for:日志数据太长的直接过滤掉
  161. } else {
  162. MethodSignature signature = (MethodSignature) joinPoint.getSignature();
  163. Method method = signature.getMethod();
  164. // 请求的方法参数值
  165. Object[] args = joinPoint.getArgs();
  166. // 请求的方法参数名称
  167. LocalVariableTableParameterNameDiscoverer u = new LocalVariableTableParameterNameDiscoverer();
  168. String[] paramNames = u.getParameterNames(method);
  169. if (args != null && paramNames != null) {
  170. for (int i = 0; i < args.length; i++) {
  171. params += " " + paramNames[i] + ": " + args[i];
  172. }
  173. }
  174. }
  175. return params;
  176. }
  177. /**
  178. * online日志内容拼接
  179. * @param obj
  180. * @param content
  181. * @return
  182. */
  183. private String getOnlineLogContent(Object obj, String content){
  184. if (Result.class.isInstance(obj)){
  185. Result res = (Result)obj;
  186. String msg = res.getMessage();
  187. String tableName = res.getOnlTable();
  188. if(oConvertUtils.isNotEmpty(tableName)){
  189. content+=",表名:"+tableName;
  190. }
  191. if(res.isSuccess()){
  192. content+= ","+(oConvertUtils.isEmpty(msg)?"操作成功":msg);
  193. }else{
  194. content+= ","+(oConvertUtils.isEmpty(msg)?"操作失败":msg);
  195. }
  196. }
  197. return content;
  198. }
  199. /* private void saveSysLog(ProceedingJoinPoint joinPoint, long time, Object obj) {
  200. MethodSignature signature = (MethodSignature) joinPoint.getSignature();
  201. Method method = signature.getMethod();
  202. SysLog sysLog = new SysLog();
  203. AutoLog syslog = method.getAnnotation(AutoLog.class);
  204. if(syslog != null){
  205. //update-begin-author:taoyan date:
  206. String content = syslog.value();
  207. if(syslog.module()== ModuleType.ONLINE){
  208. content = getOnlineLogContent(obj, content);
  209. }
  210. //注解上的描述,操作日志内容
  211. sysLog.setLogContent(content);
  212. sysLog.setLogType(syslog.logType());
  213. }
  214. //请求的方法名
  215. String className = joinPoint.getTarget().getClass().getName();
  216. String methodName = signature.getName();
  217. sysLog.setMethod(className + "." + methodName + "()");
  218. //设置操作类型
  219. if (sysLog.getLogType() == CommonConstant.LOG_TYPE_2) {
  220. sysLog.setOperateType(getOperateType(methodName, syslog.operateType()));
  221. }
  222. //获取request
  223. HttpServletRequest request = SpringContextUtils.getHttpServletRequest();
  224. //请求的参数
  225. sysLog.setRequestParam(getReqestParams(request,joinPoint));
  226. //设置IP地址
  227. sysLog.setIp(IPUtils.getIpAddr(request));
  228. //获取登录用户信息
  229. LoginUser sysUser = (LoginUser)SecurityUtils.getSubject().getPrincipal();
  230. if(sysUser!=null){
  231. sysLog.setUserid(sysUser.getUsername());
  232. sysLog.setUsername(sysUser.getRealname());
  233. }
  234. //耗时
  235. sysLog.setCostTime(time);
  236. sysLog.setCreateTime(new Date());
  237. //保存系统日志
  238. sysLogService.save(sysLog);
  239. }*/
  240. }