|
@@ -6,6 +6,7 @@ import com.yonge.cooleshow.common.entity.HttpResponseResult;
|
|
import com.yonge.toolset.base.exception.BizException;
|
|
import com.yonge.toolset.base.exception.BizException;
|
|
import com.yonge.toolset.base.exception.ThirdpartyException;
|
|
import com.yonge.toolset.base.exception.ThirdpartyException;
|
|
import com.yonge.toolset.utils.http.HttpUtil;
|
|
import com.yonge.toolset.utils.http.HttpUtil;
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
import org.apache.commons.codec.binary.Base64;
|
|
import org.apache.commons.codec.binary.Base64;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.apache.commons.lang3.exception.ExceptionUtils;
|
|
import org.apache.commons.lang3.exception.ExceptionUtils;
|
|
@@ -23,11 +24,17 @@ import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
import javax.crypto.Mac;
|
|
import javax.crypto.Mac;
|
|
import javax.crypto.spec.SecretKeySpec;
|
|
import javax.crypto.spec.SecretKeySpec;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
+import java.io.BufferedReader;
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.io.InputStreamReader;
|
|
import java.net.URLEncoder;
|
|
import java.net.URLEncoder;
|
|
|
|
+import java.util.Enumeration;
|
|
import java.util.HashMap;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
|
|
+import java.util.Objects;
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
+@Slf4j
|
|
@ControllerAdvice
|
|
@ControllerAdvice
|
|
public class BaseController {
|
|
public class BaseController {
|
|
|
|
|
|
@@ -135,9 +142,13 @@ public class BaseController {
|
|
return failed(errorMsg);
|
|
return failed(errorMsg);
|
|
}
|
|
}
|
|
try {
|
|
try {
|
|
|
|
+
|
|
|
|
+ // 机器人通知消息
|
|
|
|
+ String message = getRobotRequestLog(request);
|
|
|
|
+
|
|
Map<String, Object> paramMap = new HashMap<>(2);
|
|
Map<String, Object> paramMap = new HashMap<>(2);
|
|
JSONObject jsonObject = new JSONObject();
|
|
JSONObject jsonObject = new JSONObject();
|
|
- jsonObject.put("content", "系统繁忙请及时处理: " + request.getRequestURL() + " " + e);
|
|
|
|
|
|
+ jsonObject.put("content", "系统繁忙请及时处理: " + message + " " + e);
|
|
paramMap.put("text", jsonObject.toJSONString());
|
|
paramMap.put("text", jsonObject.toJSONString());
|
|
paramMap.put("msgtype", "text");
|
|
paramMap.put("msgtype", "text");
|
|
Map<String, String> headers = new HashMap<>(1);
|
|
Map<String, String> headers = new HashMap<>(1);
|
|
@@ -152,6 +163,91 @@ public class BaseController {
|
|
return failed("系统繁忙");
|
|
return failed("系统繁忙");
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 打印异常请求日志
|
|
|
|
+ * @param request HttpServletRequest
|
|
|
|
+ * @return String
|
|
|
|
+ */
|
|
|
|
+ private String getRobotRequestLog(HttpServletRequest request){
|
|
|
|
+
|
|
|
|
+ if (Objects.isNull(request)) {
|
|
|
|
+ log.warn("getRobotRequestLog request is null");
|
|
|
|
+ return "";
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
|
+ // 打印请求日志或执行其他逻辑
|
|
|
|
+ sb.append("Request URI: ").append(request.getRequestURI());
|
|
|
|
+
|
|
|
|
+ // 请求参数
|
|
|
|
+ Map<String, String> requestParams = getRequestParameters(request);
|
|
|
|
+ if (!requestParams.isEmpty()) {
|
|
|
|
+ sb.append(", Request Parameters: ").append(requestParams);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 请求头
|
|
|
|
+ Map<String, String> requestHeaders = getRequestHeaders(request);
|
|
|
|
+ if (!requestHeaders.isEmpty()) {
|
|
|
|
+ sb.append(", Request Headers: ").append(requestHeaders);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 请求消息体
|
|
|
|
+ String requestBody = getRequestBody(request);
|
|
|
|
+ if (!requestBody.isEmpty()) {
|
|
|
|
+ sb.append(", Request JSON: ").append(requestBody);
|
|
|
|
+ }
|
|
|
|
+ return sb.toString();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 请求参数
|
|
|
|
+ * @param request HttpServletRequest
|
|
|
|
+ * @return Map<String, String>
|
|
|
|
+ */
|
|
|
|
+ private Map<String, String> getRequestParameters(HttpServletRequest request) {
|
|
|
|
+ Map<String, String> params = new HashMap<>();
|
|
|
|
+ request.getParameterMap().forEach((name, values) -> {
|
|
|
|
+ if (values.length > 0) {
|
|
|
|
+ params.put(name, values[0]);
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ return params;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 请求头
|
|
|
|
+ * @param request HttpServletRequest
|
|
|
|
+ * @return Map<String, String>
|
|
|
|
+ */
|
|
|
|
+ private Map<String, String> getRequestHeaders(HttpServletRequest request) {
|
|
|
|
+ Enumeration<String> headerNames = request.getHeaderNames();
|
|
|
|
+ Map<String, String> headers = new HashMap<>();
|
|
|
|
+ while (headerNames.hasMoreElements()) {
|
|
|
|
+ String headerName = headerNames.nextElement();
|
|
|
|
+ String headerValue = request.getHeader(headerName);
|
|
|
|
+ headers.put(headerName, headerValue);
|
|
|
|
+ }
|
|
|
|
+ return headers;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 请求消息体
|
|
|
|
+ * @param request HttpServletRequest
|
|
|
|
+ * @return String
|
|
|
|
+ */
|
|
|
|
+ private String getRequestBody(HttpServletRequest request) {
|
|
|
|
+ StringBuilder requestBody = new StringBuilder();
|
|
|
|
+ try (BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream()))) {
|
|
|
|
+ String line;
|
|
|
|
+ while ((line = reader.readLine()) != null) {
|
|
|
|
+ requestBody.append(line);
|
|
|
|
+ }
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ log.error("robot getRequestBody body: {}", e.getMessage());
|
|
|
|
+ }
|
|
|
|
+ return requestBody.toString();
|
|
|
|
+ }
|
|
|
|
+
|
|
public String dingTalkRobotsSecurityParam() throws Exception {
|
|
public String dingTalkRobotsSecurityParam() throws Exception {
|
|
Long timestamp = System.currentTimeMillis();
|
|
Long timestamp = System.currentTimeMillis();
|
|
String secret = "SEC7371152e58a066b358afd9e9cfaad2aee4b46471218f1aeef88d43a401d8e2f2";
|
|
String secret = "SEC7371152e58a066b358afd9e9cfaad2aee4b46471218f1aeef88d43a401d8e2f2";
|