/
LicenseCreator.java
98 lines (82 loc) · 3.48 KB
/
LicenseCreator.java
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
package com.plasticene.boot.license.core;
import com.plasticene.boot.common.exception.BizException;
import com.plasticene.boot.license.core.param.CustomKeyStoreParam;
import com.plasticene.boot.license.core.param.LicenseCreatorParam;
import com.plasticene.boot.license.core.prop.LicenseProperties;
import de.schlichtherle.license.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import javax.security.auth.x500.X500Principal;
import java.io.File;
import java.util.Calendar;
import java.util.Date;
import java.util.prefs.Preferences;
/**
* @author fjzheng
* @version 1.0
* @date 2022/7/28 14:31
*/
@Component
public class LicenseCreator {
@Resource
private LicenseProperties licenseProperties;
private static Logger logger = LogManager.getLogger(LicenseCreator.class);
private final static X500Principal DEFAULT_HOLDER_AND_ISSUER = new X500Principal("CN=localhost, OU=localhost, O=localhost, L=SH, ST=SH, C=CN");
/**
* 生成License证书
*/
public boolean generateLicense(LicenseCreatorParam param){
try {
LicenseManager licenseManager = new LicenseManager(initLicenseParam());
LicenseContent licenseContent = initLicenseContent(param);
licenseManager.store(licenseContent, new File(licenseProperties.getLicensePath()));
return true;
}catch (Exception e){
logger.error("证书生成失败:", e);
throw new BizException("生成license证书失败");
}
}
/**
* 初始化证书生成参数
*/
private LicenseParam initLicenseParam(){
Preferences preferences = Preferences.userNodeForPackage(LicenseCreator.class);
//设置对证书内容加密的秘钥
CipherParam cipherParam = new DefaultCipherParam(licenseProperties.getStorePass());
KeyStoreParam privateStoreParam = new CustomKeyStoreParam(LicenseCreator.class
,licenseProperties.getPrivateKeysStorePath()
,licenseProperties.getPrivateAlias()
,licenseProperties.getStorePass()
,licenseProperties.getKeyPass());
LicenseParam licenseParam = new DefaultLicenseParam(licenseProperties.getSubject()
,preferences
,privateStoreParam
,cipherParam);
return licenseParam;
}
/**
* 设置证书生成正文信息
*/
private LicenseContent initLicenseContent(LicenseCreatorParam param){
LicenseContent licenseContent = new LicenseContent();
licenseContent.setHolder(DEFAULT_HOLDER_AND_ISSUER);
licenseContent.setIssuer(DEFAULT_HOLDER_AND_ISSUER);
licenseContent.setSubject(licenseContent.getSubject());
licenseContent.setIssued(param.getIssuedTime());
licenseContent.setNotBefore(param.getIssuedTime());
licenseContent.setNotAfter(param.getExpiryTime() == null ? addYears(new Date(), 10) : param.getExpiryTime());
licenseContent.setConsumerType(param.getConsumerType());
licenseContent.setConsumerAmount(param.getConsumerAmount());
licenseContent.setInfo(param.getDescription());
licenseContent.setExtra(param.getSystemInfo());
return licenseContent;
}
public Date addYears(Date date, int n) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.YEAR, n);
return cal.getTime();
}
}