yangys
2025-05-26 2ba2c339acf41fd7bb2a49f0ce186fd664a80cb5
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package com.qianwen.license.common;
 
import java.io.File;
import java.text.DateFormat;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.prefs.Preferences;
 
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
 
import de.schlichtherle.license.CipherParam;
import de.schlichtherle.license.DefaultCipherParam;
import de.schlichtherle.license.DefaultLicenseParam;
import de.schlichtherle.license.KeyStoreParam;
import de.schlichtherle.license.LicenseContent;
import de.schlichtherle.license.LicenseManager;
import de.schlichtherle.license.LicenseParam;
 
public class LicenseVerify {
    private static Logger logger = LogManager.getLogger(LicenseVerify.class);
    /**
     * 证书subject
     */
    //private String subject;
    /**
     * 公钥别称
     */
    //private String publicAlias;
    /**
     * 访问公钥库的密码
     */
    //private String storePass;
    /**
     * 证书生成路径
     */
    //private String licensePath;
    /**
     * 密钥库存储路径
     */
    //private String publicKeysStorePath;
    /**
     * LicenseManager
     */
    //private LicenseManager licenseManager;
    /**
     * 标识证书是否安装成功
     */
    //private boolean installSuccess;
    /**
    public LicenseVerify(String subject, String publicAlias, String storePass, String licensePath, String publicKeysStorePath) {
        this.subject = subject;
        this.publicAlias = publicAlias;
        this.storePass = storePass;
        this.licensePath = licensePath;
        this.publicKeysStorePath = publicKeysStorePath;
    }*/
 
    /**
     * 安装License证书,读取证书相关的信息, 在bean加入容器的时候自动调用
     */
    /*
    public void installLicense(LicenseParam param) {
        try {
            Preferences preferences = Preferences.userNodeForPackage(LicenseVerify.class);
 
            CipherParam cipherParam = new DefaultCipherParam(storePass);
 
            KeyStoreParam publicStoreParam = new CustomKeyStoreParam(LicenseVerify.class,
                    publicKeysStorePath,
                    publicAlias,
                    storePass,
                    null);
            LicenseParam licenseParam = new DefaultLicenseParam(subject, preferences, publicStoreParam, cipherParam);
 
            licenseManager = new CustomLicenseManager(licenseParam);
            licenseManager.uninstall();
            LicenseContent licenseContent = licenseManager.install(new File(licensePath));
            DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            installSuccess = true;
            logger.info("------------------------------- 证书安装成功 -------------------------------");
            logger.info(MessageFormat.format("证书有效期:{0} - {1},NAME={2}", format.format(licenseContent.getNotBefore()), format.format(licenseContent.getNotAfter()),licenseContent.getHolder().getName()));
        } catch (Exception e) {
            installSuccess = false;
            logger.error("证书安装失败",e);
        }
    }*/
 
    public LicenseContent install(LicenseVerifyParam param) {
         LicenseContent result = null;
        try {
            /*
            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);
            LicenseParam licenseParam = new DefaultLicenseParam(subject, preferences, publicStoreParam, cipherParam);
 
            licenseManager = new CustomLicenseManager(licenseParam);
            */
            LicenseManager licenseManager = LicenseManagerHolder.getInstance(initLicenseParam(param));
            licenseManager.uninstall();
            LicenseContent licenseContent = licenseManager.install(new File(param.getLicensePath()));
            DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            //installSuccess = true;
            logger.info("------------------------------- 证书安装成功 -------------------------------");
            logger.info(MessageFormat.format("证书有效期:{0} - {1},NAME={2}", format.format(licenseContent.getNotBefore()), format.format(licenseContent.getNotAfter()),licenseContent.getHolder().getName()));
        } catch (Exception e) {
            logger.error("证书安装失败",e);
        }
        return result;
    }
    
    
    
    
 
    public boolean verify(){
        LicenseManager licenseManager = LicenseManagerHolder.getInstance(null);
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 
        //2. 校验证书
        try {
            
            LicenseContent licenseContent = licenseManager.verify();
//            System.out.println(licenseContent.getSubject());
 
            logger.info(MessageFormat.format("证书校验通过,证书有效期:{0} - {1}",format.format(licenseContent.getNotBefore()),format.format(licenseContent.getNotAfter())));
            return true;
        }catch (Exception e){
            logger.error("证书校验失败!",e);
            return false;
        }
    }
    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);
        LicenseParam licenseParam = new DefaultLicenseParam(param.getSubject(), preferences, publicStoreParam, cipherParam);
 
        return licenseParam;
    }
 
    public LicenseContent getContent() {
        LicenseManager licenseManager = LicenseManagerHolder.getInstance(null);
        LicenseContent licenseContent = null;
        //2. 校验证书
        try {
            
            licenseContent = licenseManager.verify();
           
        }catch (Exception e){
            logger.error("证书校验失败!",e);
        }
        
        return licenseContent;
    }
   
}