package com.zy.system.timer; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.zy.common.utils.HttpHandler; import com.zy.system.entity.LicenseInfos; import com.zy.system.entity.license.*; import com.zy.system.service.LicenseInfosService; import de.schlichtherle.license.LicenseContent; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.util.Date; import java.util.HashMap; @Component public class LicenseTimer { private static boolean SYSTEM_SUPPORT = false;// 系统激活状态,默认关闭 private static int LICENSE_DAYS = 0;// 许可证天数 /** * 证书subject */ @Value("${license.subject}") private String subject; /** * 公钥别称 */ @Value("${license.publicAlias}") private String publicAlias; /** * 访问公钥库的密码 */ @Value("${license.storePass}") private String storePass; /** * 证书生成路径 */ @Value("${license.licensePath}") private String licensePath; /** * 密钥库存储路径 */ @Value("${license.publicKeysStorePath}") private String publicKeysStorePath; /** * 许可证服务端地址。 */ @Value("${license.remoteServerUrl:http://net.zoneyung.net:9999/license}") private String remoteServerUrl; @Autowired private LicenseInfosService licenseInfosService; // 每天晚上11点更新系统激活状态 @Scheduled(cron = "0 0 23 * * ? ") public void timer() { try { getRemoteLicense(); } catch (Exception e) { } try { verify(); } catch (Exception e) { } } public void getRemoteLicense() { try { String requestCode = LicenseUtils.buildRequestCode(subject); LicenseCheck serverInfos = LicenseUtils.getServerInfos(); JSONObject response = requestRemoteLicense(buildRequestCodePayload(requestCode)); if (!isSuccess(response)) { response = requestRemoteLicense(buildLegacyPayload(serverInfos)); } if (isSuccess(response)) { LicenseInfos licenseInfos = new LicenseInfos(); licenseInfos.setLicense(response.getString("data")); licenseInfos.setCreateTime(new Date()); licenseInfos.setLicenseTime(response.getString("licenseTime")); licenseInfos.setRequestCode(requestCode); licenseInfosService.insert(licenseInfos); } } catch (Exception e) { e.printStackTrace(); } } private JSONObject requestRemoteLicense(String json) { try { String response = new HttpHandler.Builder() .setUri(remoteServerUrl) .setPath("/remoteQueryLicense") .setJson(json) .build() .doPost(); if (response == null || response.trim().isEmpty()) { return null; } return JSON.parseObject(response); } catch (Exception e) { return null; } } private String buildRequestCodePayload(String requestCode) { HashMap map = new HashMap<>(); map.put("subject", subject); map.put("requestCode", requestCode); return JSON.toJSONString(map); } private String buildLegacyPayload(LicenseCheck serverInfos) { HashMap map = new HashMap<>(); map.put("subject", subject); map.put("licenseCheck", serverInfos); return JSON.toJSONString(map); } private boolean isSuccess(JSONObject jsonObject) { return jsonObject != null && "ok".equalsIgnoreCase(jsonObject.getString("result")); } public void verify() { LicenseVerifyParam param = new LicenseVerifyParam(); param.setSubject(subject); param.setPublicAlias(publicAlias); param.setStorePass(storePass); param.setLicensePath(licensePath); param.setPublicKeysStorePath(publicKeysStorePath); String requestCode = LicenseUtils.buildRequestCode(subject); LicenseInfos latestLicense = licenseInfosService.getLatestLicenseByRequestCode(requestCode); // 验证许可证是否有效 LicenseVerify licenseVerify = new LicenseVerify(); LicenseContent install = null; if (latestLicense != null) { install = licenseVerify.install(param, latestLicense.getLicense()); } if (install == null) { install = licenseVerify.install(param); } if (install != null) { Date start = new Date(); Date end = install.getNotAfter(); Long starTime = start.getTime(); Long endTime = end.getTime(); long num = endTime - starTime;// 时间戳相差的毫秒数 int day = (int) (num / 24 / 60 / 60 / 1000); setLicenseDays(day); setSystemSupport(true); } else { setLicenseDays(0); setSystemSupport(false); } } public boolean getSystemSupport() { return SYSTEM_SUPPORT; } public void setSystemSupport(boolean systemSupport) { SYSTEM_SUPPORT = systemSupport; } public int getLicenseDays() { return LICENSE_DAYS; } public void setLicenseDays(int licenseDays) { LICENSE_DAYS = licenseDays; } }