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