From f8d075d54b8f0be91e8942a24b3333ff5d4359a0 Mon Sep 17 00:00:00 2001 From: liyongde <1419499670@qq.com> Date: Tue, 23 Sep 2025 14:19:22 +0800 Subject: [PATCH] =?UTF-8?q?opt:=20=E5=86=85=E5=AD=98=E7=BC=93=E5=AD=98?= =?UTF-8?q?=E6=A8=A1=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/org/nl/core/AuthInterceptor.java | 4 +- .../java/org/nl/core/LicenseVerifier.java | 45 +++++++++++++++++-- 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/nl-verify-check-sdk/src/main/java/org/nl/core/AuthInterceptor.java b/nl-verify-check-sdk/src/main/java/org/nl/core/AuthInterceptor.java index e5b39b7..26a24f7 100644 --- a/nl-verify-check-sdk/src/main/java/org/nl/core/AuthInterceptor.java +++ b/nl-verify-check-sdk/src/main/java/org/nl/core/AuthInterceptor.java @@ -5,7 +5,6 @@ import org.springframework.web.servlet.HandlerInterceptor; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; @@ -36,8 +35,7 @@ public class AuthInterceptor implements HandlerInterceptor { if (!Files.exists(path)) { result = LicenseResult.requestResult(false, "授权码失败", null); } else { - // 考虑存内存 - String authCode = new String(Files.readAllBytes(path), StandardCharsets.UTF_8).trim(); + String authCode = verifier.getInstalledAuthCode(); result = verifier.verify(authCode); } } diff --git a/nl-verify-check-sdk/src/main/java/org/nl/core/LicenseVerifier.java b/nl-verify-check-sdk/src/main/java/org/nl/core/LicenseVerifier.java index 477eaba..6d3074a 100644 --- a/nl-verify-check-sdk/src/main/java/org/nl/core/LicenseVerifier.java +++ b/nl-verify-check-sdk/src/main/java/org/nl/core/LicenseVerifier.java @@ -3,9 +3,11 @@ package org.nl.core; import org.nl.util.RSAUtil; import java.io.IOException; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.nio.file.StandardOpenOption; import java.time.LocalDate; import java.time.format.DateTimeFormatter; @@ -18,6 +20,9 @@ public class LicenseVerifier { private final LicenseProperties properties; + // 内存缓存授权码,避免频繁读取文件 + private volatile String cachedAuthCode; + public LicenseVerifier(RSAUtil rsaUtil, LicenseProperties properties) { this.rsaUtil = rsaUtil; this.properties = properties; @@ -56,12 +61,46 @@ public class LicenseVerifier { } Files.write( targetPath, - (authCode == null ? "" : authCode).getBytes(java.nio.charset.StandardCharsets.UTF_8), - java.nio.file.StandardOpenOption.CREATE, - java.nio.file.StandardOpenOption.TRUNCATE_EXISTING + (authCode == null ? "" : authCode).getBytes(StandardCharsets.UTF_8), + StandardOpenOption.CREATE, + StandardOpenOption.TRUNCATE_EXISTING ); + // 更新内存缓存 + cachedAuthCode = authCode == null ? "" : authCode.trim(); } catch (IOException e) { throw new RuntimeException("写入授权码失败: " + e.getMessage(), e); } } + + /** + * 懒加载读取已安装授权码,优先走内存缓存。 + */ + public String getInstalledAuthCode() { + String local = cachedAuthCode; + if (local != null) { + return local; + } + if (cachedAuthCode != null) { + return cachedAuthCode; + } + String sdkLocation = properties.getSdkLocation(); + if (sdkLocation == null || sdkLocation.trim().isEmpty()) { + cachedAuthCode = null; + return null; + } + try { + Path path = Paths.get(sdkLocation); + if (!Files.exists(path)) { + cachedAuthCode = null; + return null; + } + String content = new String(Files.readAllBytes(path), StandardCharsets.UTF_8).trim(); + cachedAuthCode = content; + return cachedAuthCode; + } catch (IOException e) { + // 读取失败时,不缓存错误结果,直接返回 null + cachedAuthCode = null; + return null; + } + } }