diff --git a/wfc-common/wfc-common-license/src/main/java/org/wfc/common/license/CustomLicenseManager.java b/wfc-common/wfc-common-license/src/main/java/org/wfc/common/license/CustomLicenseManager.java index eed62fd..739f053 100644 --- a/wfc-common/wfc-common-license/src/main/java/org/wfc/common/license/CustomLicenseManager.java +++ b/wfc-common/wfc-common-license/src/main/java/org/wfc/common/license/CustomLicenseManager.java @@ -184,7 +184,7 @@ public class CustomLicenseManager extends LicenseManager { private boolean verityTimeFile() { try { // 取出时间文件 - BufferedReader reader = new BufferedReader(new FileReader(LicenseConstants.TIME_PATH)); + BufferedReader reader = new BufferedReader(new FileReader(LicenseConstants.TIME_PATH + LicenseConstants.TIME_FILE_NAME)); String fileJson = reader.readLine(); reader.close(); String decryptedJson = LicenseUtils.decrypt(fileJson); diff --git a/wfc-common/wfc-common-license/src/main/java/org/wfc/common/license/domain/LicenseConstants.java b/wfc-common/wfc-common-license/src/main/java/org/wfc/common/license/domain/LicenseConstants.java index 1b1a183..a0fdec6 100644 --- a/wfc-common/wfc-common-license/src/main/java/org/wfc/common/license/domain/LicenseConstants.java +++ b/wfc-common/wfc-common-license/src/main/java/org/wfc/common/license/domain/LicenseConstants.java @@ -6,7 +6,9 @@ package org.wfc.common.license.domain; */ public class LicenseConstants { - public final static String TIME_PATH = "/var/lib/pro/etc/serve/production.json"; + public final static String TIME_PATH = "/var/lib/pro/etc/server/"; + + public final static String TIME_FILE_NAME = "production.json"; public static final String KEY = "wfcwanfiAdmin6666"; } diff --git a/wfc-common/wfc-common-license/src/main/java/org/wfc/common/license/runner/LicenseCheckRunner.java b/wfc-common/wfc-common-license/src/main/java/org/wfc/common/license/runner/LicenseCheckRunner.java index 21b6fe1..998ed54 100644 --- a/wfc-common/wfc-common-license/src/main/java/org/wfc/common/license/runner/LicenseCheckRunner.java +++ b/wfc-common/wfc-common-license/src/main/java/org/wfc/common/license/runner/LicenseCheckRunner.java @@ -1,5 +1,6 @@ package org.wfc.common.license.runner; +import cn.hutool.core.io.FileUtil; import cn.hutool.core.util.StrUtil; import com.fasterxml.jackson.databind.ObjectMapper; import lombok.Data; @@ -15,9 +16,11 @@ import org.wfc.common.license.domain.LicenseVerifyParam; import org.wfc.common.license.serverinfo.AbstractServerInfos; import org.wfc.common.license.utils.LicenseUtils; +import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; +import java.io.FileReader; import java.io.FileWriter; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; @@ -40,7 +43,7 @@ public class LicenseCheckRunner implements ApplicationRunner { // 启动定时任务 public void startTimer() { scheduledFuture = scheduler.scheduleAtFixedRate(this::timer, 0, 30, TimeUnit.SECONDS); - scheduler.scheduleAtFixedRate(this::fileTimer, 0, 60, TimeUnit.SECONDS); + scheduler.scheduleAtFixedRate(this::fileTimer, 0, 40, TimeUnit.SECONDS); } // 停止定时任务 @@ -103,7 +106,7 @@ public class LicenseCheckRunner implements ApplicationRunner { * 5秒检测一次,不能太快也不能太慢 */ protected void fileTimer() { - createTimeFile(); + createAndVerifyTimeFile(); } private void install() { @@ -165,11 +168,39 @@ public class LicenseCheckRunner implements ApplicationRunner { } } - private void createTimeFile() { + private void createAndVerifyTimeFile() { + boolean isCreate = false; + String decryptedJson = ""; + try { + // 取出时间文件 + BufferedReader reader = new BufferedReader(new FileReader(LicenseConstants.TIME_PATH + LicenseConstants.TIME_FILE_NAME)); + String fileJson = reader.readLine(); + reader.close(); + decryptedJson = LicenseUtils.decrypt(fileJson); + } catch (Exception e) { + isCreate = true; + log.debug("first read time file error {}", e.getMessage()); + } + if (StrUtil.isBlank(decryptedJson)) { + isCreate = true; + } + if (isCreate) { + // 不存在则创建 + FileUtil.mkdir(LicenseConstants.TIME_PATH); + updateTimeFile(); + } else { + // 存在则验证时间是否被篡改,不被篡改则更新当前时间 + if (Long.parseLong(decryptedJson) < System.currentTimeMillis()) { + updateTimeFile(); + } + } + } + + private void updateTimeFile() { try { String currentTime = System.currentTimeMillis() + ""; String encryptedJson = LicenseUtils.encrypt(currentTime); - FileWriter writer = new FileWriter(LicenseConstants.TIME_PATH); + FileWriter writer = new FileWriter(LicenseConstants.TIME_PATH + LicenseConstants.TIME_FILE_NAME); writer.write(encryptedJson); writer.close(); } catch (Exception e) { @@ -223,6 +254,7 @@ public class LicenseCheckRunner implements ApplicationRunner { @Override public void run(ApplicationArguments args) throws Exception { generate(); + createAndVerifyTimeFile(); install(); startTimer(); }