cm-cloud/cloud-security/src/test/java/com/cm/LicenseTest.java

184 lines
6.5 KiB
Java
Raw Normal View History

2019-09-16 11:29:09 +08:00
package com.cm;
import com.cm.security.License;
import com.cm.security.utils.AddressUtil;
import com.cm.security.utils.AesUtil;
2020-03-27 22:27:34 +08:00
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
2019-09-16 11:29:09 +08:00
import org.junit.Test;
2020-03-27 22:27:34 +08:00
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
2019-09-16 11:29:09 +08:00
/**
* @author Administrator
* @version 1.0
* @className LicenseTest
* @description TODO
* @date 2018/11/20 14:46
*
**/
public class LicenseTest {
2020-03-27 22:27:34 +08:00
private static String LICENSE_KEY = "_System_License_";
private static String LICENSE_OLD_KDY = "CMXX0471";
2019-11-17 11:11:57 +08:00
@Test
2019-09-16 11:29:09 +08:00
public void licenseTest() throws Exception {
// 3C-A0-67-4A-88-D2
// 巴盟三务公开6C-92-BF-4F-07-0D
// 包头东河三务公开00-16-3E-00-62-F6
// 通用 00-16-3E-00-79-58
2020-04-14 18:41:10 +08:00
// 乌拉特中旗三务公开:
2019-10-12 00:09:31 +08:00
// 乌海海勃湾区 50-AF-73-27-2E-B2
// 东河新三务公开00-16-3E-00-79-BC
2020-02-17 23:21:23 +08:00
// 本机F0-79-60-1E-49-FC
2020-04-14 18:41:10 +08:00
// 应急管理局测试服务器(58.18.22.25)00-50-56-BE-3C-BC
2020-05-09 19:30:34 +08:00
// 包头政法委00-50-56-93-2D-29
2021-03-11 12:52:30 +08:00
// 内蒙古艺校84-65-69-5C-23-AA
2020-02-17 23:21:23 +08:00
// String mac = AddressUtil.getMacAddress();
// System.out.println(mac);
2020-05-09 19:30:34 +08:00
// 环保统一用户FA-16-3E-17-20-7D
2020-05-21 16:02:55 +08:00
// 包头体育局00-16-3E-00-F3-CE
2020-06-22 19:39:30 +08:00
// 包头安监局业务服务器00-50-56-93-66-AC
2020-07-14 12:52:13 +08:00
// 西藏日喀则08-94-EF-6B-5A-44
2021-05-22 09:19:54 +08:00
// 集宁环保FA-26-00-03-D3-C4
String license = License.getLicense("2021-05-06", "10000", "FA-26-00-03-D3-C4", "_System_License_");
2019-09-16 11:29:09 +08:00
System.out.println(license);
}
2020-03-27 22:27:34 +08:00
@Test
public void licenseOldTest() {
2020-04-14 18:41:10 +08:00
String license = getOldLicense("2020-04-13", "10000", "00-16-3E-00-58-B4", "CMXX0471");
2020-03-27 22:27:34 +08:00
System.out.println(license);
}
@Test
public void licenseTimeOldTest() {
System.out.println(checkOldLicense("CMXX0471", "MzZhMGQ1NmU4ZjM3Yjc5ZTRkNzk4OTkyYTQ0OWVjZmUtZjA0NTA5MWZlZDI3OWRlNDVjZDEwZmVjZWI1MTI2YjgtZDFjYTNhYWY1MmI0MWFjZDY4ZWJiM2JmNjkwNzliZDEtVGxwdFoyZG5Wa0Z0TVZSUmFuVXZOR0YyWVRSNlRsVk5hWGczY1ZkTVYyeFZNRTlrVDBwaVdIcFVjVFF6TmpCb1dqZFdiVGxWU1RRM01GUlBXRnBsTnc9PQ=="));
2019-09-16 11:29:09 +08:00
}
public void aesTest() throws Exception {
String encode = AesUtil.aesEncoder("CMXX", "123");
//String decode = AesUtil.aesDecoder("ABCDEFGHIJKLMNRS", "vqZ8mVpLg2RqdPML+9tPxma3NrQDO/pECXWvUr/RIHE=");
System.out.println(encode);
//System.out.println(decode);
}
public void webServiceTest() {
}
2020-03-27 22:27:34 +08:00
/**
* 获取旧的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<String> licenses = new ArrayList<String>();
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;
}
// macmac不匹配
List<String> 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;
}
2019-09-16 11:29:09 +08:00
}