|
@@ -10,23 +10,18 @@ import org.apache.commons.io.IOUtils;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
-import org.springframework.context.annotation.Bean;
|
|
|
|
-import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
|
|
|
import org.springframework.web.method.HandlerMethod;
|
|
import org.springframework.web.method.HandlerMethod;
|
|
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
|
|
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
|
|
|
|
|
|
-import javax.servlet.ServletException;
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
import java.nio.charset.Charset;
|
|
import java.nio.charset.Charset;
|
|
import java.text.SimpleDateFormat;
|
|
import java.text.SimpleDateFormat;
|
|
-import java.util.ArrayList;
|
|
|
|
-import java.util.Date;
|
|
|
|
-import java.util.List;
|
|
|
|
-import java.util.Map;
|
|
|
|
-import java.util.concurrent.Executor;
|
|
|
|
-import java.util.concurrent.ThreadPoolExecutor;
|
|
|
|
|
|
+import java.util.*;
|
|
|
|
+import java.util.concurrent.CompletableFuture;
|
|
|
|
+import java.util.concurrent.ExecutorService;
|
|
|
|
+import java.util.concurrent.Executors;
|
|
|
|
|
|
/**
|
|
/**
|
|
* 日志审计的拦截器
|
|
* 日志审计的拦截器
|
|
@@ -39,53 +34,25 @@ public class AuditLogInterceptor extends HandlerInterceptorAdapter {
|
|
@Value("${spring.application.name}")
|
|
@Value("${spring.application.name}")
|
|
private String clientName;
|
|
private String clientName;
|
|
|
|
|
|
- private String username;
|
|
|
|
-
|
|
|
|
- private Integer userId;
|
|
|
|
-
|
|
|
|
- private String tenantId;
|
|
|
|
-
|
|
|
|
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
|
|
|
private static List<String> ignoreLogUrl;
|
|
private static List<String> ignoreLogUrl;
|
|
|
|
|
|
- /**
|
|
|
|
- * 异步线程池配置
|
|
|
|
- */
|
|
|
|
- @Bean("syncSaveLog")
|
|
|
|
- public Executor asyncExecutor() {
|
|
|
|
- ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
|
|
|
|
- // 设置核心线程数
|
|
|
|
- executor.setCorePoolSize(10);
|
|
|
|
- // 设置最大线程数
|
|
|
|
- executor.setMaxPoolSize(100);
|
|
|
|
- // 设置队列容量
|
|
|
|
- executor.setQueueCapacity(1000);
|
|
|
|
- // 设置线程活跃时间(秒)
|
|
|
|
- executor.setKeepAliveSeconds(60);
|
|
|
|
- // 设置默认线程名称
|
|
|
|
- executor.setThreadNamePrefix("syncSaveLog-");
|
|
|
|
- // 设置拒绝策略
|
|
|
|
- executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
|
|
|
|
- // 等待所有任务结束后再关闭线程池
|
|
|
|
- executor.setWaitForTasksToCompleteOnShutdown(true);
|
|
|
|
- executor.initialize();
|
|
|
|
- return executor;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- @Override
|
|
|
|
- public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws ServletException, IOException {
|
|
|
|
- return true;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
@Override
|
|
@Override
|
|
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
|
|
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
|
|
- syncSaveLog(request, handler);
|
|
|
|
|
|
+ ExecutorService executor = Executors.newCachedThreadPool();
|
|
|
|
+ CompletableFuture.runAsync(()->{
|
|
|
|
+ syncSaveLog(request, handler);
|
|
|
|
+ },executor);
|
|
}
|
|
}
|
|
|
|
|
|
public void syncSaveLog(HttpServletRequest request, Object handler) {
|
|
public void syncSaveLog(HttpServletRequest request, Object handler) {
|
|
try {
|
|
try {
|
|
|
|
+ Object userId = request.getAttribute("userId");
|
|
|
|
+ Object username = request.getAttribute("username");
|
|
|
|
+ Integer operator = Objects.isNull(userId)?Integer.parseInt(userId.toString()):null;
|
|
|
|
+ String name = Objects.isNull(username)?username.toString():null;
|
|
|
|
+
|
|
String servletPath = request.getServletPath();
|
|
String servletPath = request.getServletPath();
|
|
HandlerMethod handlerMethod = (HandlerMethod) handler;
|
|
HandlerMethod handlerMethod = (HandlerMethod) handler;
|
|
AuditLogAnnotation anno = handlerMethod.getMethodAnnotation(AuditLogAnnotation.class);
|
|
AuditLogAnnotation anno = handlerMethod.getMethodAnnotation(AuditLogAnnotation.class);
|
|
@@ -98,7 +65,7 @@ public class AuditLogInterceptor extends HandlerInterceptorAdapter {
|
|
operateName = anno.operateName();
|
|
operateName = anno.operateName();
|
|
}
|
|
}
|
|
if (anno != null) {
|
|
if (anno != null) {
|
|
- saveLog(operateName, servletPath, request);
|
|
|
|
|
|
+ saveLog(operateName, servletPath, request,operator,name);
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
if (servletPath.contains("/task/") || servletPath.contains("/import/")) {
|
|
if (servletPath.contains("/task/") || servletPath.contains("/import/")) {
|
|
@@ -120,13 +87,13 @@ public class AuditLogInterceptor extends HandlerInterceptorAdapter {
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- saveLog(operateName, servletPath, request);
|
|
|
|
|
|
+ saveLog(operateName, servletPath, request,operator,name);
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- private void saveLog(String operateName, String servletPath, HttpServletRequest request) throws IOException {
|
|
|
|
|
|
+ private void saveLog(String operateName, String servletPath, HttpServletRequest request,Integer userId,String username) throws IOException {
|
|
AuditLog auditLog = new AuditLog();
|
|
AuditLog auditLog = new AuditLog();
|
|
auditLog.setOperateName(operateName);
|
|
auditLog.setOperateName(operateName);
|
|
auditLog.setInterfaceUrl(servletPath);
|
|
auditLog.setInterfaceUrl(servletPath);
|
|
@@ -146,10 +113,4 @@ public class AuditLogInterceptor extends HandlerInterceptorAdapter {
|
|
auditLog.setOperateTime(sdf.format(new Date()));
|
|
auditLog.setOperateTime(sdf.format(new Date()));
|
|
auditLogService.insert(auditLog);
|
|
auditLogService.insert(auditLog);
|
|
}
|
|
}
|
|
-
|
|
|
|
- public void setUsername(String username, Integer userId, String tenantId) {
|
|
|
|
- this.username = username;
|
|
|
|
- this.userId = userId;
|
|
|
|
- this.tenantId = tenantId;
|
|
|
|
- }
|
|
|
|
}
|
|
}
|