自动化立体仓库 - WMS系统
lty
4 天以前 d174064c9c48a409d8ee6cdc1d65a5ffe1b2ed0f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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);
    }
 
    /**
     * 将Base64字符串转换为临时文件
     * @param base64String Base64编码的字符串
     * @param filePrefix 文件名前缀(例如 "license_")
     * @param fileSuffix 文件后缀(例如 ".lic")
     * @return 生成的临时File对象(自动在JVM退出时删除)
     * @throws IOException
     */
    public File base64ToTempFile(String base64String, String filePrefix, String fileSuffix)
            throws IOException {
        // 解码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");
    }
 
}