package com.qianwen.license.common;
|
|
import de.schlichtherle.license.*;
|
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.Logger;
|
|
import javax.security.auth.x500.X500Principal;
|
import java.io.File;
|
import java.text.MessageFormat;
|
import java.util.prefs.Preferences;
|
|
|
public class LicenseCreator {
|
//TODO 找个需要改掉
|
//CN=yangys, OU=qianwen, O=qianwen, L=beijing, ST=beijing, C=cn是否正确?
|
//private final static X500Principal DEFAULT_HOLDER_AND_ISSUER = new X500Principal("CN=GENMER, OU=GENM, O=GENM, L=FUZHOU, ST=FUJIAN, C=CHINA");
|
/**
|
* 发行方
|
*/
|
private final static X500Principal DEFAULT_ISSUER = new X500Principal("CN=qw, OU=qwkj, O=qwkj, L=WUXI, ST=ZHEJIANG, C=CN");
|
private final static X500Principal DEFAULT_HOLDER = new X500Principal("CN=qw, OU=qwkj, O=qwkj, L=WUXI, ST=ZHEJIANG, C=CN");
|
|
private static Logger logger = LogManager.getLogger(LicenseCreator.class);
|
|
private License license;
|
|
public LicenseCreator(License license) {
|
this.license = license;
|
}
|
|
/**
|
* 生成License证书
|
*/
|
public boolean generateLicense() {
|
try {
|
LicenseManager licenseManager = new CustomLicenseManager(initLicenseParam());
|
LicenseContent licenseContent = initLicenseContent();
|
licenseManager.store(licenseContent, new File(license.getLicensePath()));
|
return true;
|
} catch (Exception e) {
|
logger.error(MessageFormat.format("证书生成失败:{0}", license), e);
|
return false;
|
}
|
}
|
|
/**
|
* 初始化证书生成参数
|
*/
|
private LicenseParam initLicenseParam() {
|
Preferences preferences = Preferences.userNodeForPackage(LicenseCreator.class);
|
|
//设置对证书内容加密的秘钥
|
CipherParam cipherParam = new DefaultCipherParam(license.getStorePass());
|
|
KeyStoreParam privateStoreParam = new CustomKeyStoreParam(LicenseCreator.class
|
, license.getPrivateKeysStorePath()
|
, license.getPrivateAlias()
|
, license.getStorePass()
|
, license.getKeyPass());
|
|
return new DefaultLicenseParam(license.getSubject()
|
, preferences
|
, privateStoreParam
|
, cipherParam);
|
}
|
|
/**
|
* 设置证书生成正文信息
|
*/
|
private LicenseContent initLicenseContent() {
|
LicenseContent licenseContent = new LicenseContent();
|
|
String name = String.format("CN=%s, OU=%s, O=%s, L=%s, ST=%s, C=%s", license.getCn(),license.getOu(),license.getO(),license.getL(),license.getSt(),license.getC());
|
X500Principal holder = new X500Principal(name);//需要根据入参初始化
|
|
licenseContent.setHolder(holder);
|
licenseContent.setIssuer(DEFAULT_ISSUER);
|
|
licenseContent.setSubject(license.getSubject());
|
licenseContent.setIssued(license.getIssuedTime());
|
licenseContent.setNotBefore(license.getIssuedTime());
|
licenseContent.setNotAfter(license.getExpiryTime());
|
licenseContent.setConsumerType(license.getConsumerType());
|
licenseContent.setConsumerAmount(license.getConsumerAmount());
|
licenseContent.setInfo(license.getDescription());
|
|
//扩展校验,这里可以自定义一些额外的校验信息(也可以用json字符串保存)
|
if (license.getLicenseExtraModel() != null) {
|
licenseContent.setExtra(license.getLicenseExtraModel());
|
}
|
|
return licenseContent;
|
}
|
|
}
|