|
|
@@ -1,17 +1,28 @@
|
|
|
package com.zswl.dataservice.service.openApi;
|
|
|
|
|
|
-import cn.hutool.json.JSONObject;
|
|
|
+import com.zswl.dataservice.dao.openApi.BlackListDao;
|
|
|
import com.zswl.dataservice.dao.openApi.OpenApiRequestLogDao;
|
|
|
import com.zswl.dataservice.service.base.RedisService;
|
|
|
+import com.zswl.dataservice.service.user.OperationLogsService;
|
|
|
+import com.zswl.dataservice.type.OperationLogType;
|
|
|
+import com.zswl.dataservice.utils.mqtt.type.LogsLevel;
|
|
|
+import com.zswl.dataservice.utils.net.IPUtil;
|
|
|
+import com.zswl.dataservice.utils.os.SystemUtil;
|
|
|
import jakarta.servlet.http.HttpServletRequest;
|
|
|
import jakarta.servlet.http.HttpServletResponse;
|
|
|
+import lombok.SneakyThrows;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.context.ApplicationContext;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.context.request.RequestContextHolder;
|
|
|
+import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
import org.springframework.web.servlet.ModelAndView;
|
|
|
+import org.springframework.web.util.ContentCachingRequestWrapper;
|
|
|
|
|
|
-import java.util.LinkedHashMap;
|
|
|
-import java.util.Map;
|
|
|
+import java.util.concurrent.ExecutorService;
|
|
|
+import java.util.concurrent.Executors;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
/**
|
|
|
* @author TRX
|
|
|
@@ -30,33 +41,46 @@ public class OpenApiVerifyService {
|
|
|
@Autowired
|
|
|
private OpenApiRequestLogDao openApiRequestLogDao;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ OperationLogsService operationLogsService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ BlackListDao blackListDao;
|
|
|
+
|
|
|
+ //线程池
|
|
|
+ ExecutorService executorService = Executors.newFixedThreadPool(SystemUtil.getCpuCoreCount() * 2);
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private void init(ApplicationContext applicationContext) {
|
|
|
+ Runtime.getRuntime().addShutdownHook(new Thread(() -> {
|
|
|
+ executorService.shutdownNow();
|
|
|
+ }));
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
- * 验证OpenAPI
|
|
|
+ * 验证OpenAPI preHandle
|
|
|
*
|
|
|
* @param request
|
|
|
* @param response
|
|
|
* @param object
|
|
|
* @return
|
|
|
*/
|
|
|
+ @SneakyThrows
|
|
|
public boolean verify(HttpServletRequest request, HttpServletResponse response, Object object) {
|
|
|
log.info("---------------------openAPI验证----------------------");
|
|
|
- Map<String, String[]> map = request.getParameterMap();
|
|
|
- log.info("map: {}", map);
|
|
|
- log.info("tt: {}", request.getParameter("msgId"));
|
|
|
-
|
|
|
- Map<String, String> paramMap = new LinkedHashMap<>();
|
|
|
- map.forEach((String key, String[] values) -> {
|
|
|
- log.info("key: {}, values: {}", key, values);
|
|
|
- paramMap.put(key, values[0]);
|
|
|
- });
|
|
|
- JSONObject jsonObject = new JSONObject(paramMap);
|
|
|
-
|
|
|
- log.info("jsonObject: {}", jsonObject);
|
|
|
+ String method = request.getMethod();
|
|
|
+ String sessionId = request.getRequestedSessionId();
|
|
|
+ log.info("sessionId: {}", sessionId);
|
|
|
+ String remoteIp = IPUtil.getRemoteIp(request);
|
|
|
+ if (request instanceof ContentCachingRequestWrapper) {
|
|
|
+ ContentCachingRequestWrapper contentCachingRequestWrapper = (ContentCachingRequestWrapper) request;
|
|
|
+ log.info("body {}", contentCachingRequestWrapper.getContentAsString());
|
|
|
+ }
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 记录日志
|
|
|
+ * 记录日志 postHandle
|
|
|
*
|
|
|
* @param request
|
|
|
* @param response
|
|
|
@@ -64,7 +88,19 @@ public class OpenApiVerifyService {
|
|
|
* @param modelAndView
|
|
|
*/
|
|
|
public void saveLog(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
|
|
|
-
|
|
|
+ log.info("saveLog");
|
|
|
}
|
|
|
|
|
|
+ public boolean isInBlackList(HttpServletRequest request) {
|
|
|
+ String ip = IPUtil.getRemoteIp(request);
|
|
|
+ if (blackListDao.existsByIp(ip)) {
|
|
|
+ ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
|
|
+ RequestContextHolder.setRequestAttributes(servletRequestAttributes, true);//设置子线程共享
|
|
|
+ executorService.execute(() -> {
|
|
|
+ operationLogsService.addLogs(String.format("黑名单地址访问: %s", ip), LogsLevel.High, OperationLogType.Black, null);
|
|
|
+ });
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
}
|