diff --git a/cloud-security/src/test/java/com/cm/LicenseTest.java b/cloud-security/src/test/java/com/cm/LicenseTest.java index 80eb313..6eea6b8 100644 --- a/cloud-security/src/test/java/com/cm/LicenseTest.java +++ b/cloud-security/src/test/java/com/cm/LicenseTest.java @@ -3,8 +3,14 @@ package com.cm; import com.cm.security.License; import com.cm.security.utils.AddressUtil; import com.cm.security.utils.AesUtil; +import org.apache.commons.codec.binary.Base64; +import org.apache.commons.codec.digest.DigestUtils; import org.junit.Test; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.List; + /** * @author Administrator * @version 1.0 @@ -15,6 +21,9 @@ import org.junit.Test; **/ public class LicenseTest { + private static String LICENSE_KEY = "_System_License_"; + private static String LICENSE_OLD_KDY = "CMXX0471"; + @Test public void licenseTest() throws Exception { // 3C-A0-67-4A-88-D2 @@ -31,8 +40,15 @@ public class LicenseTest { System.out.println(license); } - public void licenseTimeTest() { - System.out.println(License.checkLicense("CMXX0471", "OFZRQ2JDU2lCelgwcUozaUpWL3ROZWw2Y3VsbXhrOGhCMDNDYXN3am5RWHBlbkxwWnNaUElRZE53bXJNSTUwRldRbFdiQ3JnRFoxNlJlcXpkdUxwUHhSQm9RcVFlV2pLZ1lOU2tkemNUb3lsR1l4UTRvbnBmckVEWGdRUFJkWEc=")); + @Test + public void licenseOldTest() { + String license = getOldLicense("2020-02-18", "10000000", "50-AF-73-27-2E-B2", "CMXX0471"); + System.out.println(license); + } + + @Test + public void licenseTimeOldTest() { + System.out.println(checkOldLicense("CMXX0471", "MzZhMGQ1NmU4ZjM3Yjc5ZTRkNzk4OTkyYTQ0OWVjZmUtZjA0NTA5MWZlZDI3OWRlNDVjZDEwZmVjZWI1MTI2YjgtZDFjYTNhYWY1MmI0MWFjZDY4ZWJiM2JmNjkwNzliZDEtVGxwdFoyZG5Wa0Z0TVZSUmFuVXZOR0YyWVRSNlRsVk5hWGczY1ZkTVYyeFZNRTlrVDBwaVdIcFVjVFF6TmpCb1dqZFdiVGxWU1RRM01GUlBXRnBsTnc9PQ==")); } public void aesTest() throws Exception { @@ -45,4 +61,115 @@ public class LicenseTest { public void webServiceTest() { } + + /** + * 获取旧的License + * @param startTime + * @param timeLong + * @param mac + * @param token + * @return + */ + public static String getOldLicense(String startTime, String timeLong, String mac, String token) { + String result = null; + if (null == startTime || startTime.isEmpty()) { + return result; + } + if (null == timeLong || timeLong.isEmpty()) { + return result; + } + if (null == mac || mac.isEmpty()) { + return result; + } + if (null == token || token.isEmpty()) { + return result; + } + try { + // 开始时间 + String startTimeMD5 = DigestUtils.md5Hex(startTime); + // MAC地址 + String macMD5 = DigestUtils.md5Hex(mac); + // 使用时长 + String timeMD5 = DigestUtils.md5Hex(timeLong); + // 构建license数组 + List licenses = new ArrayList(); + licenses.add(startTimeMD5); + licenses.add(macMD5); + licenses.add(timeMD5); + // 构建可解密内容 + StringBuilder licenseFullPart = new StringBuilder(); + licenseFullPart.append(startTime).append(","); + licenseFullPart.append(mac).append(","); + licenseFullPart.append(timeLong).append(",").append("0"); + // license + String licenseFull = new String(Base64.encodeBase64(AesUtil.aesEncoder(token, licenseFullPart.toString()).getBytes("UTF-8"))); + licenses.add(licenseFull); + StringBuilder license = new StringBuilder(); + for (String str : licenses) { + if (license.length() > 0) { + license.append("-"); + } + license.append(str); + } + result = new String(Base64.encodeBase64((license.toString().getBytes("UTF-8"))), "UTF-8"); + } catch (Exception e) { + e.printStackTrace(); + return result; + } + return result; + } + + public static boolean checkOldLicense(String token, String license) { + SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd"); + if (null == token || token.isEmpty()) { + return false; + } + if (null == license || license.isEmpty()) { + return false; + } + try { + byte[] licenseByte = Base64.decodeBase64(license); + String licenseFull = new String(licenseByte, "UTF-8"); + String[] checkInfos = licenseFull.split("-"); + String startTime = checkInfos[0]; + String mac = checkInfos[1]; + String time = checkInfos[2]; + String fullInfo = checkInfos[3]; + String info = new String(Base64.decodeBase64(fullInfo), "UTF-8"); + String[] checkInfo = AesUtil.aesDecoder(token, info).split(","); + // 校验时间 + if (!startTime.equals(DigestUtils.md5Hex(checkInfo[0]))) { + return false; + } + // 开始时间大于当前时间 + if (SDF.parse(checkInfo[0]).getTime() > System.currentTimeMillis()) { + return false; + } + // mac,mac不匹配 + List macIds = AddressUtil.getMacIds(); + boolean confirmMac = false; + for (String macId : macIds) { + if (checkInfo[1].equals(macId) && mac.equals(DigestUtils.md5Hex(checkInfo[1]))) { + confirmMac = true; + break; + } + } + if (!confirmMac) { + return false; + } + // 授权时长 + if (!time.equals(DigestUtils.md5Hex(checkInfo[2]))) { + return false; + } + // 当前有效时间 + if (Integer.parseInt(checkInfo[2]) < Integer.parseInt(checkInfo[3])) { + return false; + } + } catch (Exception e) { + e.printStackTrace(); + return false; + } + return true; + } + }