优化报错提醒

This commit is contained in:
沈健
2024-11-05 14:21:03 +08:00
parent cd909b7c15
commit 96c257b464
2 changed files with 41 additions and 1 deletions
@@ -27,7 +27,11 @@ public class FangjiaOpenExceptionHandler implements ExceptionHandler {
String ip = getLocalHostIP();
String token = "2307d2fec8314b3e8b4b83dd1d42da2e2051638e3e668f21e77ca01258bf682c";
DingDingMessageUtil.sendTextMessage(token, "[OpenApi] "+ip+"服务器内部错误,请及时关注!");
try {
DingDingMessageUtil.sendTextMessage(token, "[OpenApi] "+ip+"服务器内部错误,请及时关注!");
} catch (Exception e1) {
e1.printStackTrace();
}
context.getResponse().setStatus(500);
context.getResponse().setCharacterEncoding("UTF-8");
context.getResponse().setContentType("application/json;charset=UTF-8");
@@ -3,6 +3,7 @@ package com.fangjia.open.interceptors;
import com.fangjia.core.RegionData;
import com.fangjia.core.util.AnalyticsUtil;
import com.fangjia.core.util.DateUtil;
import com.fangjia.core.util.DingDingMessageUtil;
import com.fangjia.dal.common.ServerAreaData;
import com.fangjia.dal.mongo.fangjia.model.ServerArea;
import com.fangjia.dal.mongo.open.model.ApiUserAuthority;
@@ -25,6 +26,10 @@ import org.sothis.web.mvc.Interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.*;
/**
@@ -103,12 +108,43 @@ public class AuthenticationInterceptor implements Interceptor {
}
} catch (Exception e) {
String ip = getLocalHostIP();
String token = "2307d2fec8314b3e8b4b83dd1d42da2e2051638e3e668f21e77ca01258bf682c";
try {
DingDingMessageUtil.sendTextMessage(token, "[OpenApi] "+ip+"服务器内部错误,请及时关注!");
} catch (Exception e1) {
e1.printStackTrace();
}
LOGGER.error("Authentication error : ", e);
ret.setModel(BasicModel.buildServerErrorModel());
ret.setViewParam("code", HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
return ret;
}
}
private static String getLocalHostIP() {
/*k8s系统上运行时,使用hostname区分*/
if ("true".equalsIgnoreCase(System.getenv("ON_K8S"))) {
return System.getenv("HOSTNAME");
}
String result = null;
try {
Enumeration<?> enumeration = NetworkInterface.getNetworkInterfaces();
InetAddress ip;
while (enumeration.hasMoreElements()) {
NetworkInterface netInterface = (NetworkInterface) enumeration.nextElement();
Enumeration<?> addresses = netInterface.getInetAddresses();
while (addresses.hasMoreElements()) {
ip = (InetAddress) addresses.nextElement();
if (ip != null && ip instanceof Inet4Address && (ip.getHostAddress().contains("192.168.0") || ip.getHostAddress().contains("192.168.10"))) {
result = ip.getHostAddress();
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}
return result == null ? "192.168.20.6" : result;
}
private boolean isNeedToken(Authority authority) {