From 96c257b46455ded48167606ddad68a736284a228 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B2=88=E5=81=A5?= Date: Tue, 5 Nov 2024 14:21:03 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=8A=A5=E9=94=99=E6=8F=90?= =?UTF-8?q?=E9=86=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FangjiaOpenExceptionHandler.java | 6 +++- .../AuthenticationInterceptor.java | 36 +++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/projects/fangjia-open/src/main/java/com/fangjia/open/exceptionhander/FangjiaOpenExceptionHandler.java b/projects/fangjia-open/src/main/java/com/fangjia/open/exceptionhander/FangjiaOpenExceptionHandler.java index 3ec2c1237..7848e9b25 100644 --- a/projects/fangjia-open/src/main/java/com/fangjia/open/exceptionhander/FangjiaOpenExceptionHandler.java +++ b/projects/fangjia-open/src/main/java/com/fangjia/open/exceptionhander/FangjiaOpenExceptionHandler.java @@ -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"); diff --git a/projects/fangjia-open/src/main/java/com/fangjia/open/interceptors/AuthenticationInterceptor.java b/projects/fangjia-open/src/main/java/com/fangjia/open/interceptors/AuthenticationInterceptor.java index 471b6f946..b3e60dcbd 100644 --- a/projects/fangjia-open/src/main/java/com/fangjia/open/interceptors/AuthenticationInterceptor.java +++ b/projects/fangjia-open/src/main/java/com/fangjia/open/interceptors/AuthenticationInterceptor.java @@ -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) {