Preface:

A few days ago, I pulled the company’s latest code and found that it could not be started. After the project was started, the front end could not jump to the page after logging in. After analysis, I found that the request was intercepted by the interceptor and did not enter the gateway at all. Then I further found that the problem occurred when the license verification of the company’s project failed. Throws a null-pointer exception. It was later discovered that the company’s license generation was processed based on the MAC address of the IP, and then discovered that hutool toolkit version 5.6.6 was used to obtain the current MAC address. So there is a reflection of the above problems.

Problem analysis:

After a series of operations, and analysis. I found that the problem was caused by the version of the Hutool package. In order to find out why the current problem occurred, I further analyzed the difference between the 4.5.10 version used before and the current 5.6.6 version. To obtain a MAC address, obtain the IP address first. If the OBTAINED IP address does not have a network adapter, the MAC address cannot be read and a null pointer exception will be thrown.

// Get the MAC address of the current host
String mac = NetworkInterface.getByInetAddress(inetAddress).getHardwareAddress();
Copy the code

At this point, you can see that the problem is with the LocalHost you got. So let’s look at different versions of how to get localhost

/ / 4.5.10
	public static InetAddress getLocalhost(a) {
		InetAddress candidateAddress = null;
		NetworkInterface iface;
		InetAddress inetAddr;
		try {
			for (Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces(); ifaces.hasMoreElements();) {
				iface = ifaces.nextElement();
				for (Enumeration<InetAddress> inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements();) {
					inetAddr = inetAddrs.nextElement();
					if (false == inetAddr.isLoopbackAddress()) {
						if (inetAddr.isSiteLocalAddress()) {
							return inetAddr;
						} else if (null == candidateAddress) {
							// Non-site-local addresses are returned as candidate addresses
							candidateAddress = inetAddr;
						}
					}
				}
			}
		} catch (SocketException e) {
			// ignore socket exception, and return null;
		}

		if (null == candidateAddress) {
			try {
				candidateAddress = InetAddress.getLocalHost();
			} catch (UnknownHostException e) {
				// ignore}}return candidateAddress;
	}
Copy the code
/ / 5.6.6
	public static InetAddress getLocalhost(a) {
		final LinkedHashSet<InetAddress> localAddressList = localAddressList(address -> {
			// Non-loopback address, which is 127.*.*.*
			return false == address.isLoopbackAddress()
					// Non-local address 10.0.0.0 to 10.255.255.255, 172.16.0.0 to 172.31.255.255, and 192.168.0.0 to 192.168.255.255
					&& false == address.isSiteLocalAddress()
					// The value must be an IPV4 address
					&& address instanceof Inet4Address;
		});

		if (CollUtil.isNotEmpty(localAddressList)) {
			return CollUtil.get(localAddressList, 0);
		}

		try {
			return InetAddress.getLocalHost();
		} catch (UnknownHostException e) {
			// ignore
		}

		return null;
	}
Copy the code

Take a closer look at the code can find the problem and address the isSiteLocalAddress () is different. Inetaddress.getlocalhost (); inetAddress.getLocalhost (); Inetaddress.getlocalhost (); inetAddress.getLocalhost ();

So consider, the problem may appear in the system level, I use a MAC system, JDK1.8. After inquiring, I found that MAC seems to have the current problem, but Win does not, so I saw the same situation on Linux written by others on the Internet. The problem is that if the MAC and Linux operating systems use this method in JDK1.8, the obtained IP address is 127.0.0.1, but the obtained IP address is a normal Intranet IP address. The IP address obtained in the current method is the IP address obtained by ping the host name. The host name we ping is 127.0.0.1 because we can’t find the IP address of the current host name in the host file

Solutions:

  • Changing the host file is a common problem on Linux, but there are limitations to changing the host file.
  • Obtain en0 IP directly with the code, you can directly use the above hutool code.