| | |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import java.io.File; |
| | | import java.nio.file.Files; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.Base64; |
| | | import java.util.Date; |
| | | |
| | | /** |
| | |
| | | |
| | | //加载证书 |
| | | public boolean loadLicense() { |
| | | return loadLicense(true); |
| | | } |
| | | |
| | | public boolean loadLicense(boolean fetchRemote) { |
| | | if(!Cools.isEmpty(licensePath)){ |
| | | logger.info("++++++++ 开始加载许可证 ++++++++"); |
| | | |
| | | try { |
| | | licenseTimer.getRemoteLicense(); |
| | | } catch (Exception e) { |
| | | if (fetchRemote) { |
| | | try { |
| | | licenseTimer.getRemoteLicense(); |
| | | } catch (Exception e) { |
| | | } |
| | | } |
| | | |
| | | try { |
| | |
| | | param.setPublicKeysStorePath(publicKeysStorePath); |
| | | |
| | | LicenseVerify licenseVerify = new LicenseVerify(); |
| | | String requestCode = LicenseUtils.buildRequestCode(subject); |
| | | LicenseInfos latestLicense = licenseInfosService.getLatestLicenseByRequestCode(requestCode); |
| | | |
| | | LicenseInfos latestLicense = licenseInfosService.getLatestLicense(); |
| | | if (latestLicense == null) { |
| | | LicenseContent install = null; |
| | | if (latestLicense != null) { |
| | | install = licenseVerify.install(param, latestLicense.getLicense()); |
| | | } |
| | | if (install == null) { |
| | | install = licenseVerify.install(param); |
| | | if (install != null) { |
| | | cacheLocalLicense(requestCode, install); |
| | | } |
| | | } |
| | | if (install == null) { |
| | | logger.info("许可证不存在"); |
| | | return false; |
| | | } |
| | | |
| | | //安装证书 |
| | | LicenseContent install = licenseVerify.install(param, latestLicense.getLicense()); |
| | | |
| | | logger.info("++++++++ 许可证加载结束 ++++++++"); |
| | | |
| | |
| | | licenseTimer.setSystemSupport(false); |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | private void cacheLocalLicense(String requestCode, LicenseContent install) { |
| | | try { |
| | | File licenseFile = resolveLicenseFile(); |
| | | if (licenseFile == null || !licenseFile.exists()) { |
| | | return; |
| | | } |
| | | LicenseInfos licenseInfos = new LicenseInfos(); |
| | | licenseInfos.setLicense(Base64.getEncoder().encodeToString(Files.readAllBytes(licenseFile.toPath()))); |
| | | licenseInfos.setLicenseTime(formatLicenseTime(install)); |
| | | licenseInfos.setRequestCode(requestCode); |
| | | licenseInfos.setCreateTime(new Date()); |
| | | licenseInfosService.insert(licenseInfos); |
| | | } catch (Exception ignored) { |
| | | } |
| | | } |
| | | |
| | | private File resolveLicenseFile() { |
| | | try { |
| | | if (this.getClass().getClassLoader().getResource(licensePath) != null) { |
| | | return new File(this.getClass().getClassLoader().getResource(licensePath).toURI()); |
| | | } |
| | | } catch (Exception ignored) { |
| | | } |
| | | return new File(licensePath); |
| | | } |
| | | |
| | | private String formatLicenseTime(LicenseContent install) { |
| | | if (install == null) { |
| | | return ""; |
| | | } |
| | | SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| | | return format.format(install.getNotBefore()) + " - " + format.format(install.getNotAfter()); |
| | | } |
| | | } |