diff --git a/pom.xml b/pom.xml index b5dc58f..e682ff8 100644 --- a/pom.xml +++ b/pom.xml @@ -9,7 +9,7 @@ pom - 1.6.1 + 2.0 2.2 UTF-8 diff --git a/servlet-core/src/main/java/tech/smartboot/servlet/plugins/license/LicenseException.java b/servlet-core/src/main/java/tech/smartboot/servlet/plugins/license/LicenseException.java index f19ed14..b1515af 100644 --- a/servlet-core/src/main/java/tech/smartboot/servlet/plugins/license/LicenseException.java +++ b/servlet-core/src/main/java/tech/smartboot/servlet/plugins/license/LicenseException.java @@ -22,4 +22,9 @@ public LicenseException(String message) { public LicenseException(String message, Throwable cause) { super(message, cause); } + + @Override + public synchronized Throwable fillInStackTrace() { + return this; + } } diff --git a/servlet-core/src/main/java/tech/smartboot/servlet/plugins/license/LicensePlugin.java b/servlet-core/src/main/java/tech/smartboot/servlet/plugins/license/LicensePlugin.java index df5fd26..1a99237 100644 --- a/servlet-core/src/main/java/tech/smartboot/servlet/plugins/license/LicensePlugin.java +++ b/servlet-core/src/main/java/tech/smartboot/servlet/plugins/license/LicensePlugin.java @@ -13,6 +13,7 @@ import org.smartboot.http.common.enums.HttpStatus; import org.smartboot.http.common.logging.Logger; import org.smartboot.http.common.logging.LoggerFactory; +import org.smartboot.http.common.utils.StringUtils; import org.smartboot.http.server.HttpRequest; import org.smartboot.http.server.HttpResponse; import org.smartboot.http.server.HttpServerHandler; @@ -36,7 +37,7 @@ */ public class LicensePlugin extends Plugin { private static final Logger LOGGER = LoggerFactory.getLogger(LicensePlugin.class); - + private static final LicenseTO INVALID_LICENSE = new LicenseTO(); private LicenseTO licenseTO; private License license; @@ -50,7 +51,7 @@ public void initPlugin(Container containerRuntime) { containerRuntime.getConfiguration().setHttpServerHandler(new HttpServerHandler() { @Override public void handle(HttpRequest request, HttpResponse response, CompletableFuture completableFuture) throws Throwable { - if (licenseTO != null || "/favicon.ico".equals(request.getRequestURI())) { + if ((licenseTO != null && licenseTO != INVALID_LICENSE) || "/favicon.ico".equals(request.getRequestURI())) { baseHandler.handle(request, response, completableFuture); } else { try { @@ -78,6 +79,10 @@ public void onContainerInitialized(Container container) { System.out.println("\t" + ConsoleColors.RED + "ERROR:License not found, please check the license file:[ " + (isSpringBoot() ? "src/main/resources/smart-servlet/License.shield" : "${SERVLET_HOME}/conf/License.shield") + " ]." + ConsoleColors.RESET); return; } + if (licenseTO == INVALID_LICENSE) { + System.out.println("\t" + ConsoleColors.RED + "ERROR:License is invalid, please check the license file:[ " + (isSpringBoot() ? "src/main/resources/smart-servlet/License.shield" : "${SERVLET_HOME}/conf/License.shield") + " ]." + ConsoleColors.RESET); + return; + } SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("\t:: Licensed to " + ConsoleColors.BOLD + ConsoleColors.ANSI_UNDERLINE_ON + ConsoleColors.BLUE + licenseTO.getApplicant() + ConsoleColors.ANSI_RESET + " until " + ConsoleColors.BOLD + ConsoleColors.ANSI_UNDERLINE_ON + ConsoleColors.BLUE + sdf.format(new Date(licenseTO.getExpireTime())) + ConsoleColors.ANSI_RESET); System.out.println("\t:: License ID: " + ConsoleColors.BOLD + ConsoleColors.ANSI_UNDERLINE_ON + licenseTO.getSn() + ConsoleColors.RESET); @@ -126,7 +131,7 @@ private void loadLicense() { LicenseEntity entity = license.loadLicense(outputStream.toByteArray()); licenseTO = loadLicense(entity); } catch (Exception e) { - LOGGER.error("License load exception", e); + LOGGER.error("License load exception", e.getMessage()); } } @@ -136,13 +141,55 @@ private LicenseTO loadLicense(LicenseEntity entity) throws IOException { LicenseTO licenseTO = new LicenseTO(); licenseTO.setApplicant(properties.getProperty("enterprise.license.user")); licenseTO.setSn(properties.getProperty("enterprise.license.number")); - licenseTO.setCompatible(properties.getProperty("enterprise.compatible")); licenseTO.setExpireTime(entity.getExpireTime()); licenseTO.setTrialDuration(entity.getTrialDuration()); licenseTO.setContact(entity.getContact()); licenseTO.setVendor(entity.getApplicant()); + + if (!isVersionSupported(Container.VERSION.substring(1), properties.getProperty("supportVersion"))) { + return INVALID_LICENSE; + } return licenseTO; } + public static boolean isVersionSupported(String containerVersion, String supportVersion) { + if (StringUtils.isBlank(supportVersion)) { + return false; + } + // 解析支持版本范围 + String[] versionRange = supportVersion.split("~"); + String startVersion = versionRange[0]; + String endVersion = versionRange.length == 2 ? versionRange[1] : "99.99.99"; + + // 将版本号转换为整数数组 + int[] containerVersionArray = parseVersion(containerVersion); + int[] startVersionArray = parseVersion(startVersion); + int[] endVersionArray = parseVersion(endVersion); + + // 比较版本号 + return compareVersions(containerVersionArray, startVersionArray) >= 0 && compareVersions(containerVersionArray, endVersionArray) <= 0; + } + + private static int[] parseVersion(String version) { + String[] parts = version.split("\\."); + int[] versionArray = new int[parts.length]; + for (int i = 0; i < parts.length; i++) { + versionArray[i] = Integer.parseInt(parts[i]); + } + return versionArray; + } + + private static int compareVersions(int[] version1, int[] version2) { + int length = Math.max(version1.length, version2.length); + for (int i = 0; i < length; i++) { + int v1 = i < version1.length ? version1[i] : 0; + int v2 = i < version2.length ? version2[i] : 0; + if (v1 != v2) { + return v1 - v2; + } + } + return 0; + } + } diff --git a/servlet-core/src/main/java/tech/smartboot/servlet/plugins/license/LicenseTO.java b/servlet-core/src/main/java/tech/smartboot/servlet/plugins/license/LicenseTO.java index 097d6a6..1682669 100644 --- a/servlet-core/src/main/java/tech/smartboot/servlet/plugins/license/LicenseTO.java +++ b/servlet-core/src/main/java/tech/smartboot/servlet/plugins/license/LicenseTO.java @@ -36,7 +36,6 @@ public class LicenseTO { * 产品供应商 */ private String vendor; - private String compatible; public String getApplicant() { return applicant; @@ -90,11 +89,4 @@ public void setVendor(String vendor) { this.vendor = vendor; } - public String getCompatible() { - return compatible; - } - - public void setCompatible(String compatible) { - this.compatible = compatible; - } }