From 0252086b726aa364a486ba33f4ecee1572fa0b1f Mon Sep 17 00:00:00 2001 From: wenc000 <450292408@qq.com> Date: Mon, 17 Feb 2020 23:21:23 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0mac=E5=9C=B0=E5=9D=80?= =?UTF-8?q?=E5=92=8Cip=E5=9C=B0=E5=9D=80=E8=8E=B7=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/cm/common/utils/AddressUtil.java | 183 ++++++++++++++++-- .../main/java/com/cm/security/License.java | 15 +- .../com/cm/security/utils/AddressUtil.java | 141 +++++++++++++- .../src/test/java/com/cm/LicenseTest.java | 7 +- 4 files changed, 316 insertions(+), 30 deletions(-) diff --git a/cloud-common/src/main/java/com/cm/common/utils/AddressUtil.java b/cloud-common/src/main/java/com/cm/common/utils/AddressUtil.java index 6869c7b..88a8f72 100644 --- a/cloud-common/src/main/java/com/cm/common/utils/AddressUtil.java +++ b/cloud-common/src/main/java/com/cm/common/utils/AddressUtil.java @@ -2,28 +2,171 @@ package com.cm.common.utils; import java.net.InetAddress; import java.net.NetworkInterface; +import java.net.SocketException; +import java.util.ArrayList; +import java.util.Enumeration; +import java.util.List; +/** + * IP和MAC地址工具类 + */ public class AddressUtil { - /** - * 获取MAC地址 - * - * @return - * @throws Exception - */ - public static final String getMacAddress() throws Exception { - InetAddress inetAddress = InetAddress.getLocalHost(); - byte[] mac = NetworkInterface.getByInetAddress(inetAddress).getHardwareAddress(); - StringBuilder macAddress = new StringBuilder(); - for (byte macByte : mac) { - if (macAddress.length() > 0) { - macAddress.append("-"); - } - int macInt = macByte & 0xff; - String str = Integer.toHexString(macInt); - macAddress.append(str.length() == 1 ? 0 + str : str); - } - return macAddress.toString().toUpperCase(); - } + /** + * 获取MAC地址,可能会为空 + * + * @return + * @throws Exception + */ + @Deprecated + public static final String getMacAddress() throws Exception { + InetAddress inetAddress = InetAddress.getLocalHost(); + byte[] mac = NetworkInterface.getByInetAddress(inetAddress).getHardwareAddress(); + StringBuilder macAddress = new StringBuilder(); + for (byte macByte : mac) { + if (macAddress.length() > 0) { + macAddress.append("-"); + } + int macInt = macByte & 0xff; + String str = Integer.toHexString(macInt); + macAddress.append(str.length() == 1 ? 0 + str : str); + } + return macAddress.toString().toUpperCase(); + } + + /** + * 此方法描述的是:获得服务器的IP地址 + */ + public static String getLocalIP() throws Exception { + String sIP = ""; + InetAddress ip = null; + boolean bFindIP = false; + Enumeration netInterfaces = (Enumeration) NetworkInterface + .getNetworkInterfaces(); + while (netInterfaces.hasMoreElements()) { + if (bFindIP) { + break; + } + NetworkInterface ni = (NetworkInterface) netInterfaces + .nextElement(); + + Enumeration ips = ni.getInetAddresses(); + while (ips.hasMoreElements()) { + ip = (InetAddress) ips.nextElement(); + if (!ip.isLoopbackAddress() + && ip.getHostAddress().matches( + "(\\d{1,3}\\.){3}\\d{1,3}")) { + bFindIP = true; + break; + } + } + } + if (null != ip) { + sIP = ip.getHostAddress(); + } + return sIP; + } + + /** + * 此方法描述的是:获得服务器的IP地址(多网卡) + */ + public static List getLocalIPS() throws Exception { + InetAddress ip = null; + List ipList = new ArrayList(); + Enumeration netInterfaces = (Enumeration) NetworkInterface + .getNetworkInterfaces(); + while (netInterfaces.hasMoreElements()) { + NetworkInterface ni = (NetworkInterface) netInterfaces + .nextElement(); + Enumeration ips = ni.getInetAddresses(); + while (ips.hasMoreElements()) { + ip = (InetAddress) ips.nextElement(); + if (!ip.isLoopbackAddress() + && ip.getHostAddress().matches( + "(\\d{1,3}\\.){3}\\d{1,3}")) { + ipList.add(ip.getHostAddress()); + } + } + } + return ipList; + } + + /** + * 获得服务器的MAC地址 + */ + public static String getMacId() throws Exception { + String macId = ""; + InetAddress ip = null; + NetworkInterface ni = null; + boolean bFindIP = false; + Enumeration netInterfaces = (Enumeration) NetworkInterface + .getNetworkInterfaces(); + while (netInterfaces.hasMoreElements()) { + if (bFindIP) { + break; + } + ni = (NetworkInterface) netInterfaces + .nextElement(); + Enumeration ips = ni.getInetAddresses(); + while (ips.hasMoreElements()) { + ip = (InetAddress) ips.nextElement(); + // 非127.0.0.1 + if (!ip.isLoopbackAddress() + && ip.getHostAddress().matches( + "(\\d{1,3}\\.){3}\\d{1,3}")) { + bFindIP = true; + break; + } + } + } + if (null != ip) { + macId = getMacFromBytes(ni.getHardwareAddress()); + } + return macId; + } + + /** + * 获得服务器的MAC地址(多网卡) + */ + public static List getMacIds() throws Exception { + InetAddress ip = null; + NetworkInterface ni = null; + List macList = new ArrayList(); + Enumeration netInterfaces = (Enumeration) NetworkInterface + .getNetworkInterfaces(); + while (netInterfaces.hasMoreElements()) { + ni = (NetworkInterface) netInterfaces + .nextElement(); + // ----------特定情况,可以考虑用ni.getName判断 + // 遍历所有ip + Enumeration ips = ni.getInetAddresses(); + while (ips.hasMoreElements()) { + ip = (InetAddress) ips.nextElement(); + if (!ip.isLoopbackAddress() // 非127.0.0.1 + && ip.getHostAddress().matches( + "(\\d{1,3}\\.){3}\\d{1,3}")) { + macList.add(getMacFromBytes(ni.getHardwareAddress())); + } + } + } + return macList; + } + + private static String getMacFromBytes(byte[] bytes) { + StringBuffer mac = new StringBuffer(); + byte currentByte; + boolean first = false; + for (byte b : bytes) { + if (first) { + mac.append("-"); + } + currentByte = (byte) ((b & 240) >> 4); + mac.append(Integer.toHexString(currentByte)); + currentByte = (byte) (b & 15); + mac.append(Integer.toHexString(currentByte)); + first = true; + } + return mac.toString().toUpperCase(); + } } diff --git a/cloud-security/src/main/java/com/cm/security/License.java b/cloud-security/src/main/java/com/cm/security/License.java index 2ebb44b..8313101 100644 --- a/cloud-security/src/main/java/com/cm/security/License.java +++ b/cloud-security/src/main/java/com/cm/security/License.java @@ -100,15 +100,18 @@ public class License { return false; } // 开始时间大于当前时间 - if(SDF.parse(checkInfo[0]).getTime() > System.currentTimeMillis()) { + if (SDF.parse(checkInfo[0]).getTime() > System.currentTimeMillis()) { return false; } // mac,mac不匹配 - if (!checkInfo[1].equals(AddressUtil.getMacAddress())) { - return false; - } - if (!mac.equals(DigestUtils.md5Hex(checkInfo[1]))) { - return false; + List macIds = AddressUtil.getMacIds(); + for (String macId : macIds) { + if (!checkInfo[1].equals(macId)) { + return false; + } + if (!mac.equals(DigestUtils.md5Hex(checkInfo[1]))) { + return false; + } } // 授权时长 if (!time.equals(DigestUtils.md5Hex(checkInfo[2]))) { diff --git a/cloud-security/src/main/java/com/cm/security/utils/AddressUtil.java b/cloud-security/src/main/java/com/cm/security/utils/AddressUtil.java index 85a63f9..c0d985e 100644 --- a/cloud-security/src/main/java/com/cm/security/utils/AddressUtil.java +++ b/cloud-security/src/main/java/com/cm/security/utils/AddressUtil.java @@ -2,15 +2,19 @@ package com.cm.security.utils; import java.net.InetAddress; import java.net.NetworkInterface; +import java.util.ArrayList; +import java.util.Enumeration; +import java.util.List; public class AddressUtil { /** - * 获取MAC地址 + * 获取MAC地址, 可能会为空 * * @return * @throws Exception */ + @Deprecated public static final String getMacAddress() throws Exception { InetAddress inetAddress = InetAddress.getLocalHost(); byte[] mac = NetworkInterface.getByInetAddress(inetAddress).getHardwareAddress(); @@ -26,4 +30,139 @@ public class AddressUtil { return macAddress.toString().toUpperCase(); } + /** + * 此方法描述的是:获得服务器的IP地址 + */ + public static String getLocalIP() throws Exception { + String sIP = ""; + InetAddress ip = null; + boolean bFindIP = false; + Enumeration netInterfaces = (Enumeration) NetworkInterface + .getNetworkInterfaces(); + while (netInterfaces.hasMoreElements()) { + if (bFindIP) { + break; + } + NetworkInterface ni = (NetworkInterface) netInterfaces + .nextElement(); + + Enumeration ips = ni.getInetAddresses(); + while (ips.hasMoreElements()) { + ip = (InetAddress) ips.nextElement(); + if (!ip.isLoopbackAddress() + && ip.getHostAddress().matches( + "(\\d{1,3}\\.){3}\\d{1,3}")) { + bFindIP = true; + break; + } + } + } + if (null != ip) { + sIP = ip.getHostAddress(); + } + return sIP; + } + + /** + * 此方法描述的是:获得服务器的IP地址(多网卡) + */ + public static List getLocalIPS() throws Exception { + InetAddress ip = null; + List ipList = new ArrayList(); + Enumeration netInterfaces = (Enumeration) NetworkInterface + .getNetworkInterfaces(); + while (netInterfaces.hasMoreElements()) { + NetworkInterface ni = (NetworkInterface) netInterfaces + .nextElement(); + Enumeration ips = ni.getInetAddresses(); + while (ips.hasMoreElements()) { + ip = (InetAddress) ips.nextElement(); + if (!ip.isLoopbackAddress() + && ip.getHostAddress().matches( + "(\\d{1,3}\\.){3}\\d{1,3}")) { + ipList.add(ip.getHostAddress()); + } + } + } + return ipList; + } + + /** + * 获得服务器的MAC地址 + */ + public static String getMacId() throws Exception { + String macId = ""; + InetAddress ip = null; + NetworkInterface ni = null; + boolean bFindIP = false; + Enumeration netInterfaces = (Enumeration) NetworkInterface + .getNetworkInterfaces(); + while (netInterfaces.hasMoreElements()) { + if (bFindIP) { + break; + } + ni = (NetworkInterface) netInterfaces + .nextElement(); + Enumeration ips = ni.getInetAddresses(); + while (ips.hasMoreElements()) { + ip = (InetAddress) ips.nextElement(); + // 非127.0.0.1 + if (!ip.isLoopbackAddress() + && ip.getHostAddress().matches( + "(\\d{1,3}\\.){3}\\d{1,3}")) { + bFindIP = true; + break; + } + } + } + if (null != ip) { + macId = getMacFromBytes(ni.getHardwareAddress()); + } + return macId; + } + + /** + * 获得服务器的MAC地址(多网卡) + */ + public static List getMacIds() throws Exception { + InetAddress ip = null; + NetworkInterface ni = null; + List macList = new ArrayList(); + Enumeration netInterfaces = (Enumeration) NetworkInterface + .getNetworkInterfaces(); + while (netInterfaces.hasMoreElements()) { + ni = (NetworkInterface) netInterfaces + .nextElement(); + // ----------特定情况,可以考虑用ni.getName判断 + // 遍历所有ip + Enumeration ips = ni.getInetAddresses(); + while (ips.hasMoreElements()) { + ip = (InetAddress) ips.nextElement(); + if (!ip.isLoopbackAddress() // 非127.0.0.1 + && ip.getHostAddress().matches( + "(\\d{1,3}\\.){3}\\d{1,3}")) { + macList.add(getMacFromBytes(ni.getHardwareAddress())); + } + } + } + return macList; + } + + private static String getMacFromBytes(byte[] bytes) { + StringBuffer mac = new StringBuffer(); + byte currentByte; + boolean first = false; + for (byte b : bytes) { + if (first) { + mac.append("-"); + } + currentByte = (byte) ((b & 240) >> 4); + mac.append(Integer.toHexString(currentByte)); + currentByte = (byte) (b & 15); + mac.append(Integer.toHexString(currentByte)); + first = true; + } + return mac.toString().toUpperCase(); + } + } diff --git a/cloud-security/src/test/java/com/cm/LicenseTest.java b/cloud-security/src/test/java/com/cm/LicenseTest.java index af08cdc..feed536 100644 --- a/cloud-security/src/test/java/com/cm/LicenseTest.java +++ b/cloud-security/src/test/java/com/cm/LicenseTest.java @@ -23,9 +23,10 @@ public class LicenseTest { // 通用 00-16-3E-00-79-58 // 乌海海勃湾区 50-AF-73-27-2E-B2 // 东河新三务公开:00-16-3E-00-79-BC - String mac = AddressUtil.getMacAddress(); - System.out.println(mac); - String license = License.getLicense("2019-12-27", "10000", "00-16-3E-00-79-BC", "CMXX0471"); + // 本机:F0-79-60-1E-49-FC +// String mac = AddressUtil.getMacAddress(); +// System.out.println(mac); + String license = License.getLicense("2020-02-17", "1000", "F0-79-60-1E-49-FC", "CMXX0471"); System.out.println(license); }