| | |
| | | import de.schlichtherle.license.*; |
| | | import org.apache.logging.log4j.LogManager; |
| | | import org.apache.logging.log4j.Logger; |
| | | import org.apache.tomcat.util.http.fileupload.IOUtils; |
| | | |
| | | import java.io.File; |
| | | import java.io.FileOutputStream; |
| | | import java.io.InputStream; |
| | | import java.io.IOException; |
| | | import java.nio.file.Files; |
| | | import java.nio.file.Path; |
| | | import java.text.DateFormat; |
| | | import java.text.MessageFormat; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.Base64; |
| | | import java.util.prefs.Preferences; |
| | | |
| | | /** |
| | |
| | | /** |
| | | * 安装License证书 |
| | | */ |
| | | public synchronized LicenseContent install(LicenseVerifyParam param){ |
| | | public synchronized LicenseContent install(LicenseVerifyParam param, String license) { |
| | | LicenseContent result = null; |
| | | DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| | | |
| | | //1. 安装证书 |
| | | try{ |
| | | LicenseManager licenseManager = LicenseManagerHolder.getInstance(initLicenseParam(param)); |
| | | try { |
| | | LicenseParam licenseParam = initLicenseParam(param); |
| | | LicenseManager licenseManager = LicenseManagerHolder.getInstance(licenseParam); |
| | | licenseManager.uninstall(); |
| | | |
| | | InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(param.getLicensePath()); |
| | | File file = new File(param.getLicensePath()); |
| | | try (FileOutputStream out = new FileOutputStream(file)) { |
| | | IOUtils.copy(inputStream, out); |
| | | } |
| | | |
| | | result = licenseManager.install(new File(param.getLicensePath())); |
| | | logger.info(MessageFormat.format("许可证加载成功,许可证有效期:{0} - {1}",format.format(result.getNotBefore()),format.format(result.getNotAfter()))); |
| | | }catch (Exception e){ |
| | | logger.error("许可证加载失败!",e); |
| | | File tempFileFromBase64 = createTempFileFromBase64(license); |
| | | result = licenseManager.install(tempFileFromBase64); |
| | | logger.info(MessageFormat.format("许可证加载成功,许可证有效期:{0} - {1}", format.format(result.getNotBefore()), format.format(result.getNotAfter()))); |
| | | } catch (Exception e) { |
| | | logger.error("许可证加载失败!", e); |
| | | } |
| | | |
| | | return result; |
| | |
| | | try { |
| | | LicenseManager licenseManager = LicenseManagerHolder.getInstance(null); |
| | | DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| | | |
| | | if (!updateSystemTime()) { |
| | | //时间更新失败,系统时间被更改 |
| | | return false; |
| | | } |
| | | |
| | | LicenseContent licenseContent = licenseManager.verify(); |
| | | logger.info(MessageFormat.format("许可证校验通过,许可证有效期:{0} - {1}",format.format(licenseContent.getNotBefore()),format.format(licenseContent.getNotAfter()))); |
| | |
| | | */ |
| | | public LicenseContent getVerifyInfo(){ |
| | | LicenseManager licenseManager = LicenseManagerHolder.getInstance(null); |
| | | |
| | | if (!updateSystemTime()) { |
| | | //时间更新失败,系统时间被更改 |
| | | return null; |
| | | } |
| | | |
| | | //校验证书 |
| | | try { |
| | |
| | | } |
| | | |
| | | /** |
| | | * 更新时间到注册表中 |
| | | * 将Base64字符串转换为临时文件 |
| | | * @param base64String Base64编码的字符串 |
| | | * @param filePrefix 文件名前缀(例如 "license_") |
| | | * @param fileSuffix 文件后缀(例如 ".lic") |
| | | * @return 生成的临时File对象(自动在JVM退出时删除) |
| | | * @throws IOException |
| | | */ |
| | | private boolean updateSystemTime() { |
| | | // 获取用户根节点 |
| | | Preferences userRoot = Preferences.userRoot(); |
| | | // 获取指定路径下的节点 |
| | | Preferences node = userRoot.node("/zhongyang"); |
| | | String key = "time"; |
| | | // 读取注册表 |
| | | String value = node.get(key, null); |
| | | if (value != null) { |
| | | long originTime = Long.parseLong(value); |
| | | long now = System.currentTimeMillis(); |
| | | long diff = now - originTime;//现在时间 - 源时间 = 时间差 |
| | | if (diff > 0) { |
| | | //时间差大于0才允许更新注册表时间 |
| | | node.put(key, String.valueOf(System.currentTimeMillis())); |
| | | return true; |
| | | } |
| | | }else { |
| | | // 写入注册表 |
| | | node.put(key, String.valueOf(System.currentTimeMillis())); |
| | | return true; |
| | | } |
| | | return false; |
| | | public File base64ToTempFile(String base64String, String filePrefix, String fileSuffix) |
| | | throws IOException { |
| | | // 解码Base64 |
| | | byte[] decodedBytes = Base64.getDecoder().decode(base64String); |
| | | // 创建临时文件 |
| | | Path tempPath = Files.createTempFile(filePrefix, fileSuffix); |
| | | // 写入内容 |
| | | Files.write(tempPath, decodedBytes); |
| | | // 设置JVM退出时自动删除 |
| | | tempPath.toFile().deleteOnExit(); |
| | | return tempPath.toFile(); |
| | | } |
| | | |
| | | public File createTempFileFromBase64(String base64Data) throws IOException { |
| | | return base64ToTempFile(base64Data, "temp_license_", ".bin"); |
| | | } |
| | | |
| | | } |