Commit b1503e5a7fc9f0ac392104331ea4cc9018035367

Authored by 谭毅彬
1 parent f69e3e6d

linux ip地址验证

Signed-off-by: TanYibin <5491541@qq.com>
huaheng-wms-core/src/main/java/org/jeecg/JeecgSystemApplication.java
1 package org.jeecg; 1 package org.jeecg;
2 2
  3 +import java.net.Inet4Address;
  4 +import java.net.Inet6Address;
3 import java.net.InetAddress; 5 import java.net.InetAddress;
4 import java.net.NetworkInterface; 6 import java.net.NetworkInterface;
  7 +import java.net.SocketException;
5 import java.net.UnknownHostException; 8 import java.net.UnknownHostException;
6 import java.util.Enumeration; 9 import java.util.Enumeration;
7 10
  11 +import javax.transaction.SystemException;
  12 +
8 import org.jeecg.common.util.oConvertUtils; 13 import org.jeecg.common.util.oConvertUtils;
9 import org.springframework.boot.SpringApplication; 14 import org.springframework.boot.SpringApplication;
10 //import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 15 //import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
@@ -32,7 +37,7 @@ public class JeecgSystemApplication extends SpringBootServletInitializer { @@ -32,7 +37,7 @@ public class JeecgSystemApplication extends SpringBootServletInitializer {
32 return application.sources(JeecgSystemApplication.class); 37 return application.sources(JeecgSystemApplication.class);
33 } 38 }
34 39
35 - public static void main(String[] args) throws UnknownHostException { 40 + public static void main(String[] args) throws SystemException, UnknownHostException {
36 ConfigurableApplicationContext application = SpringApplication.run(JeecgSystemApplication.class, args); 41 ConfigurableApplicationContext application = SpringApplication.run(JeecgSystemApplication.class, args);
37 Environment env = application.getEnvironment(); 42 Environment env = application.getEnvironment();
38 String ip = getLocalHostExactAddress().getHostAddress(); 43 String ip = getLocalHostExactAddress().getHostAddress();
@@ -46,40 +51,29 @@ public class JeecgSystemApplication extends SpringBootServletInitializer { @@ -46,40 +51,29 @@ public class JeecgSystemApplication extends SpringBootServletInitializer {
46 + "WEB External: \thttp://" + ip + ":" + port + path + "/index.html\n\t" 51 + "WEB External: \thttp://" + ip + ":" + port + path + "/index.html\n\t"
47 + "API External: \thttp://" + ip + ":" + port + path + "/\n\n\t" 52 + "API External: \thttp://" + ip + ":" + port + path + "/\n\n\t"
48 + "Swagger文档: \thttp://" + ip + ":" + port + path + "/doc.html\n\n\t" 53 + "Swagger文档: \thttp://" + ip + ":" + port + path + "/doc.html\n\n\t"
49 - + "The following profiles are active: " + profiles + "\n" 54 + + "The following profiles are active: [" + profiles + "]\n"
50 + "----------------------------------------------------------"); 55 + "----------------------------------------------------------");
51 } 56 }
52 - 57 +
53 public static InetAddress getLocalHostExactAddress() { 58 public static InetAddress getLocalHostExactAddress() {
54 try { 59 try {
55 - InetAddress candidateAddress = null; 60 + InetAddress inetAddress = null;
56 61
57 Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces(); 62 Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
58 while (networkInterfaces.hasMoreElements()) { 63 while (networkInterfaces.hasMoreElements()) {
59 - NetworkInterface iface = networkInterfaces.nextElement();  
60 - // 该网卡接口下的ip会有多个,也需要一个个的遍历,找到自己所需要的  
61 - for (Enumeration<InetAddress> inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements();) {  
62 - InetAddress inetAddr = inetAddrs.nextElement();  
63 - // 排除loopback回环类型地址(不管是IPv4还是IPv6 只要是回环地址都会返回true)  
64 - if (!inetAddr.isLoopbackAddress()) {  
65 - if (inetAddr.isSiteLocalAddress()) {  
66 - // 如果是site-local地址,就是它了 就是我们要找的  
67 - // ~~~~~~~~~~~~~绝大部分情况下都会在此处返回你的ip地址值~~~~~~~~~~~~~  
68 - return inetAddr;  
69 - }  
70 -  
71 - // 若不是site-local地址 那就记录下该地址当作候选  
72 - if (candidateAddress == null) {  
73 - candidateAddress = inetAddr;  
74 - }  
75 - 64 + NetworkInterface netInterface = networkInterfaces.nextElement();
  65 + // System.out.println(netInterface.getName());
  66 + Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
  67 + while (addresses.hasMoreElements()) {
  68 + InetAddress ipTmp = addresses.nextElement();
  69 + if (ipTmp != null && ipTmp instanceof Inet4Address && ipTmp.isSiteLocalAddress() && !ipTmp.isLoopbackAddress()
  70 + && ipTmp.getHostAddress().indexOf(":") == -1) {
  71 + inetAddress = ipTmp;
76 } 72 }
77 } 73 }
78 } 74 }
79 -  
80 - // 如果出去loopback回环地之外无其它地址了,那就回退到原始方案吧  
81 - return candidateAddress == null ? InetAddress.getLocalHost() : candidateAddress;  
82 - } catch (Exception e) { 75 + return inetAddress == null ? InetAddress.getLocalHost() : inetAddress;
  76 + } catch (SocketException | UnknownHostException e) {
83 e.printStackTrace(); 77 e.printStackTrace();
84 } 78 }
85 return null; 79 return null;