From 48c1de18235020edff108339ed1d12bade8a2b90 Mon Sep 17 00:00:00 2001
From: Junjie <DELL@qq.com>
Date: 星期一, 08 十二月 2025 16:37:02 +0800
Subject: [PATCH] #

---
 src/main/java/com/zy/system/entity/license/LicenseVerify.java |  126 ++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 126 insertions(+), 0 deletions(-)

diff --git a/src/main/java/com/zy/system/entity/license/LicenseVerify.java b/src/main/java/com/zy/system/entity/license/LicenseVerify.java
new file mode 100644
index 0000000..da239c0
--- /dev/null
+++ b/src/main/java/com/zy/system/entity/license/LicenseVerify.java
@@ -0,0 +1,126 @@
+package com.zy.system.entity.license;
+
+import de.schlichtherle.license.*;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.text.DateFormat;
+import java.text.MessageFormat;
+import java.text.SimpleDateFormat;
+import java.util.Base64;
+import java.util.prefs.Preferences;
+
+/**
+ * License鏍¢獙绫�
+ */
+public class LicenseVerify {
+    private static Logger logger = LogManager.getLogger(LicenseVerify.class);
+
+    /**
+     * 瀹夎License璇佷功
+     */
+    public synchronized LicenseContent install(LicenseVerifyParam param, String license) {
+        LicenseContent result = null;
+        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+
+        //1. 瀹夎璇佷功
+        try {
+            LicenseParam licenseParam = initLicenseParam(param);
+            LicenseManager licenseManager = LicenseManagerHolder.getInstance(licenseParam);
+            licenseManager.uninstall();
+
+            File tempFileFromBase64 = createTempFileFromBase64(license);
+            result = licenseManager.install(tempFileFromBase64);
+            logger.info(MessageFormat.format("璁稿彲璇佸姞杞芥垚鍔燂紝璁稿彲璇佹湁鏁堟湡锛歿0} - {1}", format.format(result.getNotBefore()), format.format(result.getNotAfter())));
+        } catch (Exception e) {
+            logger.error("璁稿彲璇佸姞杞藉け璐ワ紒", e);
+        }
+
+        return result;
+    }
+
+    /**
+     * 鏍¢獙License璇佷功
+     */
+    public boolean verify(){
+        try {
+            LicenseManager licenseManager = LicenseManagerHolder.getInstance(null);
+            DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+
+            LicenseContent licenseContent = licenseManager.verify();
+            logger.info(MessageFormat.format("璁稿彲璇佹牎楠岄�氳繃锛岃鍙瘉鏈夋晥鏈燂細{0} - {1}",format.format(licenseContent.getNotBefore()),format.format(licenseContent.getNotAfter())));
+            return true;
+        }catch (Exception e){
+            logger.error("璁稿彲璇佹牎楠屽け璐ワ紒",e);
+            return false;
+        }
+    }
+
+    /**
+     * 鏍¢獙License璇佷功骞惰幏鍙栬瘉涔︿俊鎭�
+     */
+    public LicenseContent getVerifyInfo(){
+        LicenseManager licenseManager = LicenseManagerHolder.getInstance(null);
+
+        //鏍¢獙璇佷功
+        try {
+            LicenseContent licenseContent = licenseManager.verify();
+            return licenseContent;
+        }catch (Exception e){
+            logger.error("璁稿彲璇佹牎楠屽け璐ワ紒",e);
+            return null;
+        }
+    }
+
+    /**
+     * 鍒濆鍖栬瘉涔︾敓鎴愬弬鏁�
+     * @param param License鏍¢獙绫婚渶瑕佺殑鍙傛暟
+     * @return de.schlichtherle.license.LicenseParam
+     */
+    private LicenseParam initLicenseParam(LicenseVerifyParam param){
+        Preferences preferences = Preferences.userNodeForPackage(LicenseVerify.class);
+
+        CipherParam cipherParam = new DefaultCipherParam(param.getStorePass());
+
+        KeyStoreParam publicStoreParam = new CustomKeyStoreParam(LicenseVerify.class
+                ,param.getPublicKeysStorePath()
+                ,param.getPublicAlias()
+                ,param.getStorePass()
+                ,null);
+
+        return new DefaultLicenseParam(param.getSubject()
+                ,preferences
+                ,publicStoreParam
+                ,cipherParam);
+    }
+
+    /**
+     * 灏咮ase64瀛楃涓茶浆鎹负涓存椂鏂囦欢
+     * @param base64String Base64缂栫爜鐨勫瓧绗︿覆
+     * @param filePrefix 鏂囦欢鍚嶅墠缂�锛堜緥濡� "license_"锛�
+     * @param fileSuffix 鏂囦欢鍚庣紑锛堜緥濡� ".lic"锛�
+     * @return 鐢熸垚鐨勪复鏃禙ile瀵硅薄锛堣嚜鍔ㄥ湪JVM閫�鍑烘椂鍒犻櫎锛�
+     * @throws IOException
+     */
+    public File base64ToTempFile(String base64String, String filePrefix, String fileSuffix)
+            throws IOException {
+        // 瑙g爜Base64
+        byte[] decodedBytes = Base64.getDecoder().decode(base64String);
+        // 鍒涘缓涓存椂鏂囦欢
+        Path tempPath = Files.createTempFile(filePrefix, fileSuffix);
+        // 鍐欏叆鍐呭
+        Files.write(tempPath, decodedBytes);
+        // 璁剧疆JVM閫�鍑烘椂鑷姩鍒犻櫎
+        tempPath.toFile().deleteOnExit();
+        return tempPath.toFile();
+    }
+
+    public File createTempFileFromBase64(String base64Data) throws IOException {
+        return base64ToTempFile(base64Data, "temp_license_", ".bin");
+    }
+
+}
\ No newline at end of file

--
Gitblit v1.9.1