Commit b1503e5a7fc9f0ac392104331ea4cc9018035367
1 parent
f69e3e6d
linux ip地址验证
Signed-off-by: TanYibin <5491541@qq.com>
Showing
1 changed file
with
19 additions
and
25 deletions
huaheng-wms-core/src/main/java/org/jeecg/JeecgSystemApplication.java
1 | 1 | package org.jeecg; |
2 | 2 | |
3 | +import java.net.Inet4Address; | |
4 | +import java.net.Inet6Address; | |
3 | 5 | import java.net.InetAddress; |
4 | 6 | import java.net.NetworkInterface; |
7 | +import java.net.SocketException; | |
5 | 8 | import java.net.UnknownHostException; |
6 | 9 | import java.util.Enumeration; |
7 | 10 | |
11 | +import javax.transaction.SystemException; | |
12 | + | |
8 | 13 | import org.jeecg.common.util.oConvertUtils; |
9 | 14 | import org.springframework.boot.SpringApplication; |
10 | 15 | //import org.springframework.boot.autoconfigure.EnableAutoConfiguration; |
... | ... | @@ -32,7 +37,7 @@ public class JeecgSystemApplication extends SpringBootServletInitializer { |
32 | 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 | 41 | ConfigurableApplicationContext application = SpringApplication.run(JeecgSystemApplication.class, args); |
37 | 42 | Environment env = application.getEnvironment(); |
38 | 43 | String ip = getLocalHostExactAddress().getHostAddress(); |
... | ... | @@ -46,40 +51,29 @@ public class JeecgSystemApplication extends SpringBootServletInitializer { |
46 | 51 | + "WEB External: \thttp://" + ip + ":" + port + path + "/index.html\n\t" |
47 | 52 | + "API External: \thttp://" + ip + ":" + port + path + "/\n\n\t" |
48 | 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 | 58 | public static InetAddress getLocalHostExactAddress() { |
54 | 59 | try { |
55 | - InetAddress candidateAddress = null; | |
60 | + InetAddress inetAddress = null; | |
56 | 61 | |
57 | 62 | Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces(); |
58 | 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 | 77 | e.printStackTrace(); |
84 | 78 | } |
85 | 79 | return null; |
... | ... |