| | |
| | | package com.zy.system.timer; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.core.common.Cools; |
| | | import com.zy.common.utils.HttpHandler; |
| | | import com.zy.system.entity.LicenseInfos; |
| | | import com.zy.system.entity.license.LicenseUtils; |
| | | import com.zy.system.entity.license.LicenseVerify; |
| | | import com.zy.system.entity.license.LicenseVerifyParam; |
| | | 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 int LICENSE_DAYS = 0;//许可证天数 |
| | | |
| | | @Value("${license.subject}") |
| | | private String subject; |
| | | |
| | | @Value("${license.publicAlias}") |
| | | private String publicAlias; |
| | | |
| | | @Value("${license.storePass}") |
| | | private String storePass; |
| | | |
| | | @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() { |
| | | // System.out.println(SYSTEM_SUPPORT); |
| | | //验证许可证是否有效 |
| | | try { |
| | | getRemoteLicense(); |
| | | } catch (Exception ignored) { |
| | | } |
| | | |
| | | try { |
| | | verify(); |
| | | } catch (Exception ignored) { |
| | | } |
| | | } |
| | | |
| | | public void getRemoteLicense() { |
| | | try { |
| | | String requestCode = LicenseUtils.buildRequestCode(subject); |
| | | JSONObject response = requestRemoteLicense(buildRequestCodePayload(requestCode)); |
| | | if (isSuccess(response)) { |
| | | String license = response.getString("data"); |
| | | if (Cools.isEmpty(license)) { |
| | | return; |
| | | } |
| | | LicenseInfos latestLicense = licenseInfosService.getLatestLicenseByRequestCode(requestCode); |
| | | if (latestLicense != null && Cools.eq(latestLicense.getLicense(), license)) { |
| | | return; |
| | | } |
| | | LicenseInfos licenseInfos = new LicenseInfos(); |
| | | licenseInfos.setLicense(license); |
| | | 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<String, Object> map = new HashMap<>(); |
| | | map.put("subject", subject); |
| | | map.put("requestCode", requestCode); |
| | | 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.setPublicKeysStorePath(publicKeysStorePath); |
| | | |
| | | String requestCode = LicenseUtils.buildRequestCode(subject); |
| | | LicenseInfos latestLicense = licenseInfosService.getLatestLicenseByRequestCode(requestCode); |
| | | |
| | | LicenseVerify licenseVerify = new LicenseVerify(); |
| | | boolean verify = licenseVerify.verify(); |
| | | setSystemSupport(verify);//更新系统激活状态 |
| | | LicenseContent install = null; |
| | | if (latestLicense != null && !Cools.isEmpty(latestLicense.getLicense())) { |
| | | install = licenseVerify.install(param, latestLicense.getLicense()); |
| | | } |
| | | |
| | | if (install != null) { |
| | | Date start = new Date(); |
| | | Date end = install.getNotAfter(); |
| | | long num = end.getTime() - start.getTime(); |
| | | int day = (int) (num / 24 / 60 / 60 / 1000); |
| | | setLicenseDays(day); |
| | | setSystemSupport(true); |
| | | } else { |
| | | setLicenseDays(0); |
| | | setSystemSupport(false); |
| | | } |
| | | } |
| | | |
| | | public boolean getSystemSupport() { |