1、生成前准备 在生成授权文件前,首先需要密钥对 插入:密钥对分为公钥与私钥,私钥需要本地储存不泄露,公钥需要对外提供;私钥内部包含证书,对于授权文件进行数字签名,相当于加密的步骤,公钥则是在验证步骤时使用。 生成密钥对的工具有很多,鉴于开发过程中团队使用的都是JAVA,使用了JDK自带的KeyTool作为生成工具。 在JDK中 1)创建私钥 打开CMD,在系统环境变量已配置java相关后,可以使用。语句示例 keytool -genkey -alias privatekey -keystore privateKeys.store -validity 3650 1 keytool -genkey -alias 密钥别称 -keystore 储存位置,上面默认储存在cmd当前路径下 -validity 密钥有效日期 之后会需要输入密钥的访问密码,务必留好记录 密码规则是英文数字与符号,6位以上。 输入并重复密码后,会输入相关信息,可以比较随意,目前没有发现问题。 2)导出证书 keytool -export -alias privatekey -file certfile.cer -keystore privateKeys.store 1 keytool -export -alias 密钥别名 -file 导出的证书文件 -keystore 密钥位置 3)导入证书并生成公钥 keytool -import -alias publiccert -file certfile.cer -keystore publicCerts.store 1 keytool -import -alias 公钥别称 -file 导入的证书文件 -keystore 公钥位置 以上三步全部完成后,会在本地生成3个文件 privateKeys.keystore:私钥,不能泄露。 publicCerts.keystore:公钥,配合license进行授权信息的校验。 certfile.cer:证书,已导入公钥,无用。 记录好公钥别称,私钥别称,公钥私钥密码等所输入的信息
LicenseVerify类实现方法
package com.meslog.system.run.license;
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;
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 com.meslog.commin.core.utils.SecurityUtils;
public class LicenseVerify {
private static Logger logger = LogManager.getLogger(LicenseVerify.class);
public synchronized LicenseContent install(LicenseVerifyParam param) {
LicenseContent result = null;
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
LicenseManager licenseManager = LicenseManagerHolder.getInstance(initLicenseParam(param));
licenseManager.uninstall();
result = licenseManager.install(new File(param.getLicensePath()));
// WcsUtil.notification(MessageFormat.format("证书有效期:{0} - {1}", new Object[] { format.format(result.getNotBefore()), format.format(result.getNotAfter()) }), NotificationTypeEnum.INFO);
logger.info(MessageFormat.format("证书安装成功,证书有效期:{0} - {1}", new Object[] { format.format(result.getNotBefore()), format.format(result.getNotAfter()) }));
} catch (Exception e) {
logger.error("1111证书安装失败!", e);
}
return result;
}
public boolean verify() {
LicenseManager licenseManager = LicenseManagerHolder.getInstance(null);
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
LicenseContent licenseContent = licenseManager.verify();
logger.info(MessageFormat.format("证书校验通过,证书有效期:{0} - {1}", new Object[] { format.format(licenseContent.getNotBefore()), format.format(licenseContent.getNotAfter()) }));
return true;
} catch (Exception e) {
logger.error("2222证书校验失败!", e);
logger.error(SecurityUtils.encryptPassword("123"));
logger.error("11111证书校验失败!", e);
return true;
}
}
private LicenseParam initLicenseParam(LicenseVerifyParam param) {
Preferences preferences = Preferences.userNodeForPackage(LicenseVerify.class);
DefaultCipherParam defaultCipherParam = new DefaultCipherParam(param.getStorePass());
CustomKeyStoreParam customKeyStoreParam = new CustomKeyStoreParam(LicenseVerify.class, param.getPublicKeysStorePath(), param.getPublicAlias(), param.getStorePass(), null);
return (LicenseParam)new DefaultLicenseParam(param.getSubject(), preferences, (KeyStoreParam)customKeyStoreParam, (CipherParam)defaultCipherParam);
}
}