Browse Source

补充core-solar解析授权文件绑定版本的逻辑

Signed-off-by: luoshi <luoshi@qmth.com.cn>
luoshi 2 years ago
parent
commit
b86631db9d

+ 10 - 0
core-solar/src/main/java/com/qmth/boot/core/solar/config/SolarProperties.java

@@ -16,6 +16,8 @@ public class SolarProperties {
 
     private String appCode;
 
+    private String appVersion;
+
     private String accessKey;
 
     private String accessSecret;
@@ -38,6 +40,14 @@ public class SolarProperties {
         this.appCode = appCode;
     }
 
+    public String getAppVersion() {
+        return appVersion;
+    }
+
+    public void setAppVersion(String appVersion) {
+        this.appVersion = appVersion;
+    }
+
     public String getAccessKey() {
         return accessKey;
     }

+ 31 - 10
core-solar/src/main/java/com/qmth/boot/core/solar/crypto/AppLicenseUtil.java

@@ -40,9 +40,9 @@ public class AppLicenseUtil {
      * @param filePath
      * @return
      */
-    public static AppLicense parseLicense(String filePath) {
+    public static AppLicense parseLicense(String filePath, String version) {
         try {
-            return parseLicense(ByteArray.fromFile(new File(filePath)).value());
+            return parseLicense(ByteArray.fromFile(new File(filePath)).value(), version);
         } catch (Exception e) {
             log.error("Solar license parse error", e);
         }
@@ -50,22 +50,43 @@ public class AppLicenseUtil {
     }
 
     /**
-     * 直接解析许可证文件内容
+     * 解析授权文件获得授权信息
      *
-     * @param data
+     * @param data    授权文件二进制内容
+     * @param version 版本号格式为x.x.x
      * @return
      */
-    public static AppLicense parseLicense(byte[] data) {
+    public static AppLicense parseLicense(byte[] data, String version) {
         try {
             byte[] original = ArrayUtils.subarray(data, 1, data.length);
-            //是否绑定硬件
+            // 不限制设备
             if (data[0] == 0) {
-                //不绑定硬件
-            } else if (data[0] == 1) {
-                //获取当前设备标识
+                // 不绑定硬件
+            }
+            //限制设备,不限制版本
+            else if (data[0] == 1) {
+                // 获取当前设备标识
                 String deviceId = DeviceInfo.current().uuid();
-                //使用设备标识进行AES解密
+                // 使用设备标识进行AES解密
                 original = AES.decrypt(original, deviceId.substring(0, 16), deviceId.substring(16)).value();
+            }
+            //不限制设备,限制版本
+            else if (data[0] == 2) {
+                // 获取当前版本号MD5
+                String versionMd5 = ByteArray.md5(version).toHexString();
+                // 使用版本号MD5进行AES解密
+                original = AES.decrypt(original, versionMd5.substring(0, 16), versionMd5.substring(16)).value();
+            }
+            //限制设备,限制版本
+            else if (data[0] == 3) {
+                // 获取当前设备标识
+                String deviceId = DeviceInfo.current().uuid();
+                // 获取当前版本号MD5
+                String versionMd5 = ByteArray.md5(version).toHexString();
+                // 使用版本号MD5与设备标识进行两次AES解密
+                original = AES
+                        .decrypt(AES.decrypt(original, versionMd5.substring(0, 16), versionMd5.substring(16)).value(),
+                                deviceId.substring(0, 16), deviceId.substring(16)).value();
             } else {
                 return null;
             }

+ 4 - 2
core-solar/src/main/java/com/qmth/boot/core/solar/service/SolarService.java

@@ -110,7 +110,8 @@ public class SolarService {
         license = null;
         solarProperties.setAccessKey(null);
         solarProperties.setAccessSecret(null);
-        AppLicense result = AppLicenseUtil.parseLicense(licenseData);
+        AppLicense result = AppLicenseUtil
+                .parseLicense(licenseData, StringUtils.trimToEmpty(solarProperties.getAppVersion()));
         if (result != null && checkAppCode(result) && !result.getControl().hasExpired()) {
             license = result;
             appInfo = result;
@@ -138,7 +139,8 @@ public class SolarService {
             }
         } else if (StringUtils.isNotBlank(solarProperties.getLicense())) {
             //启动授权文件默认绑定硬件信息
-            AppLicense license = AppLicenseUtil.parseLicense(solarProperties.getLicense());
+            AppLicense license = AppLicenseUtil.parseLicense(solarProperties.getLicense(),
+                    StringUtils.trimToEmpty(solarProperties.getAppVersion()));
             if (license != null) {
                 if (!checkAppCode(license)) {
                     log.error("Solar license appCode invalid");