TrueLicense实现授权管理

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);
  }
}


 

  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

!chen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值