From 6622fe20e5839430d47952492663db704cc8bccb Mon Sep 17 00:00:00 2001 From: Francis Dong Date: Tue, 5 Jul 2022 18:49:02 +0800 Subject: [PATCH] fix local ip issue of NetworkUtils #440 --- .../src/main/java/we/util/NetworkUtils.java | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/fizz-common/src/main/java/we/util/NetworkUtils.java b/fizz-common/src/main/java/we/util/NetworkUtils.java index a8b34f5..dfd0184 100644 --- a/fizz-common/src/main/java/we/util/NetworkUtils.java +++ b/fizz-common/src/main/java/we/util/NetworkUtils.java @@ -27,6 +27,7 @@ import java.net.SocketException; import java.net.UnknownHostException; import java.security.SecureRandom; import java.util.Enumeration; +import java.util.Iterator; import java.util.LinkedHashSet; import java.util.Optional; import java.util.Set; @@ -48,21 +49,28 @@ public class NetworkUtils { private static Set serverIps = new LinkedHashSet<>(); private static final String SERVER_IP = "SERVER_IP"; + + private static final String LOCAL_IP = "127.0.0.1"; private NetworkUtils() { } /** - * @return user settings, or the first one in ip address list. + * @return user settings, or the first non local IP of IP address list. */ public static String getServerIp() { if (serverIp == null) { - serverIp = getServerIps().iterator().next(); + for (Iterator iterator = getServerIps().iterator(); iterator.hasNext();) { + serverIp = iterator.next(); + if (!LOCAL_IP.equals(serverIp)) { + break; + } + } } return serverIp; } - public static Set getServerIps() { + public synchronized static Set getServerIps() { if (serverIps.isEmpty()) { try { String ip = System.getProperty(SERVER_IP);