From 1ce5c023cf8fe78849fba9fe0f7c0fcfac09c491 Mon Sep 17 00:00:00 2001 From: jasmine <362055143@qq.com> Date: Thu, 17 Apr 2025 06:29:34 +0000 Subject: [PATCH] =?UTF-8?q?refactor(license):=20=E4=BC=98=E5=8C=96=20licen?= =?UTF-8?q?se=20=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- continew-starter-bom/pom.xml | 12 +++ .../core/constant/PropertiesConstants.java | 15 ++++ continew-starter-dependencies/pom.xml | 18 +++++ .../README.md | 10 +-- .../continew-starter-license-generate/pom.xml | 45 +---------- .../LicenseGenerateAutoConfiguration.java | 38 --------- .../LicenseGenerateAutoConfiguration.java | 57 +++++++++++++ .../LicenseGenerateProperties.java} | 30 +++---- .../license/service/LicenseCreateService.java | 36 ++++++--- .../continew/license/util/ExecCmdUtil.java | 6 +- ...ot.autoconfigure.AutoConfiguration.imports | 2 +- .../continew-starter-license-verify/README.md | 10 +-- .../continew-starter-license-verify/pom.xml | 35 +------- .../LicenseStarterInitializingBean.java | 22 ++--- .../LicenseVerifyAutoConfiguration.java | 73 +++++++++++++++++ .../LicenseVerifyProperties.java | 58 ++++++++++++++ .../license/bean/LicenseInstallerBean.java | 38 ++------- .../config/LicenseAutoConfiguration.java | 51 ------------ .../license/manager/CustomLicenseManager.java | 80 +++++++------------ ...ot.autoconfigure.AutoConfiguration.imports | 3 +- .../src/main/resources/default-license.yml | 2 - continew-starter-license/pom.xml | 10 ++- 22 files changed, 345 insertions(+), 306 deletions(-) delete mode 100644 continew-starter-license/continew-starter-license-generate/src/main/java/top/continew/license/autoConfiguration/LicenseGenerateAutoConfiguration.java create mode 100644 continew-starter-license/continew-starter-license-generate/src/main/java/top/continew/license/autoconfigure/LicenseGenerateAutoConfiguration.java rename continew-starter-license/{continew-starter-license-verify/src/main/java/top/continew/license/config/LicenseVerifyProperties.java => continew-starter-license-generate/src/main/java/top/continew/license/autoconfigure/LicenseGenerateProperties.java} (59%) create mode 100644 continew-starter-license/continew-starter-license-verify/src/main/java/top/continew/license/autoconfigure/LicenseVerifyAutoConfiguration.java create mode 100644 continew-starter-license/continew-starter-license-verify/src/main/java/top/continew/license/autoconfigure/LicenseVerifyProperties.java delete mode 100644 continew-starter-license/continew-starter-license-verify/src/main/java/top/continew/license/config/LicenseAutoConfiguration.java delete mode 100644 continew-starter-license/continew-starter-license-verify/src/main/resources/default-license.yml diff --git a/continew-starter-bom/pom.xml b/continew-starter-bom/pom.xml index ecb6b75a..ffb0dbb8 100644 --- a/continew-starter-bom/pom.xml +++ b/continew-starter-bom/pom.xml @@ -276,6 +276,18 @@ continew-starter-core ${revision} + + + top.continew + continew-starter-license-generate + ${revision} + + + + top.continew + continew-starter-license-verify + ${revision} + diff --git a/continew-starter-core/src/main/java/top/continew/starter/core/constant/PropertiesConstants.java b/continew-starter-core/src/main/java/top/continew/starter/core/constant/PropertiesConstants.java index d4ae8c2d..b787f65f 100644 --- a/continew-starter-core/src/main/java/top/continew/starter/core/constant/PropertiesConstants.java +++ b/continew-starter-core/src/main/java/top/continew/starter/core/constant/PropertiesConstants.java @@ -139,6 +139,21 @@ public class PropertiesConstants { */ public static final String TRACE = CONTINEW_STARTER + StringConstants.DOT + "trace"; + /** + * license配置 + */ + public static final String LICENSE = CONTINEW_STARTER + StringConstants.DOT + "license"; + + /** + * license 生成模块配置 + */ + public static final String LICENSE_GENERATE = LICENSE + StringConstants.DOT + "generate"; + + /** + * license 校验模块配置 + */ + public static final String LICENSE_VERIFY = LICENSE + StringConstants.DOT + "verify"; + private PropertiesConstants() { } } diff --git a/continew-starter-dependencies/pom.xml b/continew-starter-dependencies/pom.xml index 1fd77345..23608a9f 100644 --- a/continew-starter-dependencies/pom.xml +++ b/continew-starter-dependencies/pom.xml @@ -48,6 +48,10 @@ 1.9.4 2.17.0 1.26.0 + + 1.33 + 2.11.5 + 1.7.0 2.44.3 @@ -370,6 +374,20 @@ ${commons-compress.version} + + + de.schlichtherle.truelicense + truelicense-core + ${truelicense.version} + + + + + net.lingala.zip4j + zip4j + ${zip4j.version} + + top.continew diff --git a/continew-starter-license/continew-starter-license-generate/README.md b/continew-starter-license/continew-starter-license-generate/README.md index c53f6069..922cf32f 100644 --- a/continew-starter-license/continew-starter-license-generate/README.md +++ b/continew-starter-license/continew-starter-license-generate/README.md @@ -1,4 +1,4 @@ -## continew-starter-license-generate 食用方法 +## continew-starter-license-generate 使用方法 1. 引入依赖 @@ -66,7 +66,8 @@ public class LicenseGenerateController { paramVO.setStorePass("123456a"); //设置过期时间 Calendar calendar = Calendar.getInstance(); - long expire = new Date().getTime() + (24L * 3600L * 1000L); + long expire = Instant.now().toEpochMilli() + (24L * 3600L * 1000L);; + // long expire = new Date().getTime() + (24L * 3600L * 1000L); calendar.setTimeInMillis(expire); paramVO.setExpireTime(calendar.getTime()); //设置额外校验参数(服务器信息) @@ -78,7 +79,4 @@ public class LicenseGenerateController { } ``` - - - -注:默认生成 license 为C:/license下。将压缩包发送给客户端使用。 \ No newline at end of file +注:默认生成 license 为FileUtil.getTmpDirPath()下。将压缩包发送给客户端使用。 \ No newline at end of file diff --git a/continew-starter-license/continew-starter-license-generate/pom.xml b/continew-starter-license/continew-starter-license-generate/pom.xml index 6ee8ee29..e678f793 100644 --- a/continew-starter-license/continew-starter-license-generate/pom.xml +++ b/continew-starter-license/continew-starter-license-generate/pom.xml @@ -13,64 +13,23 @@ jar license 生成模块 基于 truelicens 实现 - - 1.33 - 2.2.6 - - - - - junit - junit - - - org.springframework.boot - spring-boot-starter-test - test - - + top.continew continew-starter-log-core + de.schlichtherle.truelicense truelicense-core - ${truelicense.version} net.lingala.zip4j zip4j - ${zip4j.version} - - - - - com.fasterxml.jackson.core - jackson-databind - - - com.fasterxml.jackson.core - jackson-core - - - com.fasterxml.jackson.core - jackson-annotations - - - - - org.apache.commons - commons-lang3 - - - org.springframework.boot - spring-boot-starter-test - test \ No newline at end of file diff --git a/continew-starter-license/continew-starter-license-generate/src/main/java/top/continew/license/autoConfiguration/LicenseGenerateAutoConfiguration.java b/continew-starter-license/continew-starter-license-generate/src/main/java/top/continew/license/autoConfiguration/LicenseGenerateAutoConfiguration.java deleted file mode 100644 index 00330231..00000000 --- a/continew-starter-license/continew-starter-license-generate/src/main/java/top/continew/license/autoConfiguration/LicenseGenerateAutoConfiguration.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. - *

- * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * http://www.gnu.org/licenses/lgpl.html - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package top.continew.license.autoConfiguration; - -import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import top.continew.license.service.LicenseCreateService; - -/** - * @Desc: - * @Author loach - * @ClassName top.continew.license.AutoConfiguration.LicenseGenerateAutoConfiguration - * @Date 2025-03-23 10:57 - */ -@Configuration -public class LicenseGenerateAutoConfiguration { - - @Bean - @ConditionalOnMissingBean - public LicenseCreateService licenseCreateService() { - return LicenseCreateService.getInstance(); - } -} diff --git a/continew-starter-license/continew-starter-license-generate/src/main/java/top/continew/license/autoconfigure/LicenseGenerateAutoConfiguration.java b/continew-starter-license/continew-starter-license-generate/src/main/java/top/continew/license/autoconfigure/LicenseGenerateAutoConfiguration.java new file mode 100644 index 00000000..13a61b45 --- /dev/null +++ b/continew-starter-license/continew-starter-license-generate/src/main/java/top/continew/license/autoconfigure/LicenseGenerateAutoConfiguration.java @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package top.continew.license.autoconfigure; + +import jakarta.annotation.PostConstruct; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.autoconfigure.AutoConfiguration; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; + +import top.continew.license.service.LicenseCreateService; +import top.continew.starter.core.constant.PropertiesConstants; + +/** + * license 生成模块 自动配置 + * + * @author loach + * @since 1.2.0 + */ +@AutoConfiguration +@EnableConfigurationProperties(LicenseGenerateProperties.class) +@ConditionalOnProperty(prefix = PropertiesConstants.LICENSE_GENERATE, name = PropertiesConstants.ENABLED, havingValue = "true", matchIfMissing = true) +public class LicenseGenerateAutoConfiguration { + + private static final Logger log = LoggerFactory.getLogger(LicenseGenerateAutoConfiguration.class); + + /** + * license 生成服务接口 + */ + @Bean + @ConditionalOnMissingBean + public LicenseCreateService licenseCreateService() { + return LicenseCreateService.getInstance(); + } + + @PostConstruct + public void postConstruct() { + log.debug("[ContiNew Starter] - Auto Configuration 'License-Generate' completed initialization."); + } +} diff --git a/continew-starter-license/continew-starter-license-verify/src/main/java/top/continew/license/config/LicenseVerifyProperties.java b/continew-starter-license/continew-starter-license-generate/src/main/java/top/continew/license/autoconfigure/LicenseGenerateProperties.java similarity index 59% rename from continew-starter-license/continew-starter-license-verify/src/main/java/top/continew/license/config/LicenseVerifyProperties.java rename to continew-starter-license/continew-starter-license-generate/src/main/java/top/continew/license/autoconfigure/LicenseGenerateProperties.java index 4078fa53..59592078 100644 --- a/continew-starter-license/continew-starter-license-verify/src/main/java/top/continew/license/config/LicenseVerifyProperties.java +++ b/continew-starter-license/continew-starter-license-generate/src/main/java/top/continew/license/autoconfigure/LicenseGenerateProperties.java @@ -14,27 +14,29 @@ * limitations under the License. */ -package top.continew.license.config; +package top.continew.license.autoconfigure; import org.springframework.boot.context.properties.ConfigurationProperties; +import top.continew.starter.core.constant.PropertiesConstants; /** - * @Desc: - * @Author loach - * @ClassName top.continew.license.config.LicenseYmlConfig - * @Date 2025-04-14 14:56 + * license 生成模块配置属性 + * + * @author Jasmine + * @since 1.2.0 */ -@ConfigurationProperties(prefix = "license") -public class LicenseVerifyProperties { +@ConfigurationProperties(PropertiesConstants.LICENSE_GENERATE) +public class LicenseGenerateProperties { + /** + * 是否启用 + */ + private boolean enabled = true; - private String savePath; - - public String getSavePath() { - return savePath; + public boolean isEnabled() { + return enabled; } - public void setSavePath(String savePath) { - this.savePath = savePath; + public void setEnabled(boolean enabled) { + this.enabled = enabled; } - } diff --git a/continew-starter-license/continew-starter-license-generate/src/main/java/top/continew/license/service/LicenseCreateService.java b/continew-starter-license/continew-starter-license-generate/src/main/java/top/continew/license/service/LicenseCreateService.java index 73733ae5..ea84e622 100644 --- a/continew-starter-license/continew-starter-license-generate/src/main/java/top/continew/license/service/LicenseCreateService.java +++ b/continew-starter-license/continew-starter-license-generate/src/main/java/top/continew/license/service/LicenseCreateService.java @@ -16,12 +16,33 @@ package top.continew.license.service; -import com.fasterxml.jackson.databind.ObjectMapper; -import de.schlichtherle.license.*; -import net.lingala.zip4j.ZipFile; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; +import java.util.prefs.Preferences; + +import javax.security.auth.x500.X500Principal; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; + +import com.fasterxml.jackson.databind.ObjectMapper; + +import de.schlichtherle.license.CipherParam; +import de.schlichtherle.license.DefaultCipherParam; +import de.schlichtherle.license.DefaultLicenseParam; +import de.schlichtherle.license.KeyStoreParam; +import de.schlichtherle.license.LicenseContent; +import de.schlichtherle.license.LicenseManager; +import de.schlichtherle.license.LicenseParam; +import net.lingala.zip4j.ZipFile; import top.continew.license.dto.ConfigParam; import top.continew.license.dto.LicenseCreatorParam; import top.continew.license.dto.LicenseCreatorParamVO; @@ -32,13 +53,6 @@ import top.continew.license.manager.ServerLicenseManager; import top.continew.license.util.ExecCmdUtil; import top.continew.license.util.ServerInfoUtils; -import javax.security.auth.x500.X500Principal; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.util.*; -import java.util.prefs.Preferences; - /** * 证书生成接口 实现类 * @@ -114,7 +128,7 @@ public class LicenseCreateService { String privateAlias = customerName + "-private-alias"; String publicAlias = customerName + "-public-alias"; String relativePath = relativePath(paramVO); - String currentCustomerDir = relativePath + customerName + uuid() + "/"; + String currentCustomerDir = relativePath + customerName + uuid() + File.separator; File file = new File(currentCustomerDir); if (!file.exists()) { file.mkdirs(); diff --git a/continew-starter-license/continew-starter-license-generate/src/main/java/top/continew/license/util/ExecCmdUtil.java b/continew-starter-license/continew-starter-license-generate/src/main/java/top/continew/license/util/ExecCmdUtil.java index 1babb3e0..ea6fb98a 100644 --- a/continew-starter-license/continew-starter-license-generate/src/main/java/top/continew/license/util/ExecCmdUtil.java +++ b/continew-starter-license/continew-starter-license-generate/src/main/java/top/continew/license/util/ExecCmdUtil.java @@ -16,13 +16,13 @@ package top.continew.license.util; -import org.apache.commons.lang3.ArrayUtils; - import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.LineNumberReader; +import cn.hutool.core.util.ArrayUtil; + /** * 运行命令行工具类 * @@ -50,7 +50,7 @@ public class ExecCmdUtil { process = Runtime.getRuntime().exec(cmd); } } else { - cmd = ArrayUtils.addAll(new String[] {"/bin/sh", "-c"}, cmd); + cmd = ArrayUtil.addAll(new String[] {"/bin/sh", "-c"}, cmd); process = Runtime.getRuntime().exec(cmd); } diff --git a/continew-starter-license/continew-starter-license-generate/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/continew-starter-license/continew-starter-license-generate/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports index f4f62765..ba26ebbe 100644 --- a/continew-starter-license/continew-starter-license-generate/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports +++ b/continew-starter-license/continew-starter-license-generate/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -1 +1 @@ -top.continew.license.autoConfiguration.LicenseGenerateAutoConfiguration \ No newline at end of file +top.continew.license.autoconfigure.LicenseGenerateAutoConfiguration \ No newline at end of file diff --git a/continew-starter-license/continew-starter-license-verify/README.md b/continew-starter-license/continew-starter-license-verify/README.md index 5ace45ea..78674a3f 100644 --- a/continew-starter-license/continew-starter-license-verify/README.md +++ b/continew-starter-license/continew-starter-license-verify/README.md @@ -13,10 +13,10 @@ 2. 配置YML(license 压缩包存放位置) ```yaml -license: - savePath: D:/license/ +continew-starter: + license: + verify: + storePath: D:/license ``` - - -注:默认加载 `D:/license/` 位置。 \ No newline at end of file +注:默认加载 `FileUtil.getTmpDirPath()` 位置。 \ No newline at end of file diff --git a/continew-starter-license/continew-starter-license-verify/pom.xml b/continew-starter-license/continew-starter-license-verify/pom.xml index 807d5f05..d8df226a 100644 --- a/continew-starter-license/continew-starter-license-verify/pom.xml +++ b/continew-starter-license/continew-starter-license-verify/pom.xml @@ -13,54 +13,23 @@ jar license 校验模块 基于 truelicens 实现 - - 1.33 - 2.2.6 - - - - - junit - junit - - - + top.continew continew-starter-log-core - - org.springframework.boot - spring-boot-starter - - + de.schlichtherle.truelicense truelicense-core - ${truelicense.version} net.lingala.zip4j zip4j - ${zip4j.version} - - - - - com.fasterxml.jackson.core - jackson-databind - - - com.fasterxml.jackson.core - jackson-core - - - com.fasterxml.jackson.core - jackson-annotations \ No newline at end of file diff --git a/continew-starter-license/continew-starter-license-verify/src/main/java/top/continew/license/Initializing/LicenseStarterInitializingBean.java b/continew-starter-license/continew-starter-license-verify/src/main/java/top/continew/license/Initializing/LicenseStarterInitializingBean.java index 707ddf63..159bc6d7 100644 --- a/continew-starter-license/continew-starter-license-verify/src/main/java/top/continew/license/Initializing/LicenseStarterInitializingBean.java +++ b/continew-starter-license/continew-starter-license-verify/src/main/java/top/continew/license/Initializing/LicenseStarterInitializingBean.java @@ -17,31 +17,25 @@ package top.continew.license.Initializing; import org.springframework.beans.factory.InitializingBean; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.context.properties.EnableConfigurationProperties; -import org.springframework.context.annotation.Configuration; + import top.continew.license.bean.LicenseInstallerBean; /** * 启动校验 License * - * @Desc: - * @Author loach - * @ClassName top.continew.license.Initializing.LicenseStarterAutoConfiguration - * @Date 2025-04-11 15:40 + * @author loach + * @since 1.2.0 */ -@Configuration -@EnableConfigurationProperties(LicenseStarterInitializingBean.class) public class LicenseStarterInitializingBean implements InitializingBean { + private final LicenseInstallerBean licenseInstallerBean; - @Autowired - private LicenseInstallerBean licenseInstallerBean; + public LicenseStarterInitializingBean(LicenseInstallerBean licenseInstallerBean) { + this.licenseInstallerBean = licenseInstallerBean; + } @Override public void afterPropertiesSet() throws Exception { - - //安装证书,即校验客户机器参数是否符合证书要求,符合则安装成功,不符合则报错无法启动。 + // 安装证书,即校验客户机器参数是否符合证书要求,符合则安装成功,不符合则报错无法启动。 licenseInstallerBean.installLicense(); - } } diff --git a/continew-starter-license/continew-starter-license-verify/src/main/java/top/continew/license/autoconfigure/LicenseVerifyAutoConfiguration.java b/continew-starter-license/continew-starter-license-verify/src/main/java/top/continew/license/autoconfigure/LicenseVerifyAutoConfiguration.java new file mode 100644 index 00000000..e74e955f --- /dev/null +++ b/continew-starter-license/continew-starter-license-verify/src/main/java/top/continew/license/autoconfigure/LicenseVerifyAutoConfiguration.java @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package top.continew.license.autoconfigure; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.autoconfigure.AutoConfiguration; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.DependsOn; + +import de.schlichtherle.license.LicenseManager; +import jakarta.annotation.PostConstruct; +import top.continew.license.Initializing.LicenseStarterInitializingBean; +import top.continew.license.bean.LicenseInstallerBean; +import top.continew.license.manager.CustomLicenseManager; +import top.continew.starter.core.constant.PropertiesConstants; + +/** + * license 校验模块 自动配置 + * + * @author loach + * @since 1.2.0 + */ +@AutoConfiguration +@EnableConfigurationProperties(LicenseVerifyProperties.class) +@ConditionalOnProperty(prefix = PropertiesConstants.LICENSE_VERIFY, name = PropertiesConstants.ENABLED, havingValue = "true", matchIfMissing = true) +public class LicenseVerifyAutoConfiguration { + + private static final Logger log = LoggerFactory.getLogger(LicenseVerifyAutoConfiguration.class); + + /** 证书安装业务类 */ + @Bean + public LicenseInstallerBean licenseInstallerBean(LicenseVerifyProperties properties) { + return new LicenseInstallerBean(properties); + } + + /** + * 启动校验 License服务 + */ + @Bean + @DependsOn("licenseInstallerBean") + public LicenseStarterInitializingBean licenseStarterInitializingBean(LicenseInstallerBean licenseInstallerBean) { + return new LicenseStarterInitializingBean(licenseInstallerBean); + } + + /** 客户端证书管理类(证书验证) */ + @Bean + public LicenseManager licenseManager(LicenseVerifyProperties properties) { + return CustomLicenseManager.getInstance(properties); + } + + @PostConstruct + public void postConstruct() { + log.debug("[ContiNew Starter] - Auto Configuration 'License-Verify' completed initialization."); + } + +} diff --git a/continew-starter-license/continew-starter-license-verify/src/main/java/top/continew/license/autoconfigure/LicenseVerifyProperties.java b/continew-starter-license/continew-starter-license-verify/src/main/java/top/continew/license/autoconfigure/LicenseVerifyProperties.java new file mode 100644 index 00000000..1e574170 --- /dev/null +++ b/continew-starter-license/continew-starter-license-verify/src/main/java/top/continew/license/autoconfigure/LicenseVerifyProperties.java @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package top.continew.license.autoconfigure; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +import cn.hutool.core.io.FileUtil; +import top.continew.starter.core.constant.PropertiesConstants; + +/** + * license 校验模块配置属性 + * + * @author loach + * @since 1.2.0 + */ +@ConfigurationProperties(PropertiesConstants.LICENSE_VERIFY) +public class LicenseVerifyProperties { + + /** + * 是否启用 + */ + private boolean enabled = true; + + /** + * 生成的license文件所在路径 + */ + private String storePath = FileUtil.getTmpDirPath(); + + public boolean isEnabled() { + return enabled; + } + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } + + public String getStorePath() { + return storePath; + } + + public void setStorePath(String storePath) { + this.storePath = storePath; + } +} diff --git a/continew-starter-license/continew-starter-license-verify/src/main/java/top/continew/license/bean/LicenseInstallerBean.java b/continew-starter-license/continew-starter-license-verify/src/main/java/top/continew/license/bean/LicenseInstallerBean.java index 00c2c550..2f81a674 100644 --- a/continew-starter-license/continew-starter-license-verify/src/main/java/top/continew/license/bean/LicenseInstallerBean.java +++ b/continew-starter-license/continew-starter-license-verify/src/main/java/top/continew/license/bean/LicenseInstallerBean.java @@ -19,7 +19,7 @@ package top.continew.license.bean; import de.schlichtherle.license.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import top.continew.license.config.LicenseVerifyProperties; +import top.continew.license.autoconfigure.LicenseVerifyProperties; import top.continew.license.exception.VerifyException; import top.continew.license.manager.CustomLicenseManager; @@ -28,45 +28,28 @@ import java.io.*; /** * 证书安装业务类 * - * @Desc: - * @Author loach - * @ClassName top.continew.license.bean.LicenseInstallerBean - * @Date 2025-04-15 15:05 + * @author loach + * @since 1.2.0 */ public class LicenseInstallerBean { private static final Logger log = LoggerFactory.getLogger(LicenseInstallerBean.class); - private String licensePath; - private LicenseManager licenseManager; private LicenseVerifyProperties properties; public LicenseInstallerBean(LicenseVerifyProperties properties) { this.properties = properties; - - if (properties == null || properties.getSavePath() == null) { - String os = System.getProperty("os.name"); - if (os.toLowerCase().contains("windows")) { - this.licensePath = "D:/license/"; - } - this.licensePath = "/data/license/"; - } else { - this.licensePath = properties.getSavePath(); - - } } - //安装证书 + // 安装证书 public void installLicense() throws Exception { - try { - licenseManager = CustomLicenseManager.getInstance(properties); licenseManager.uninstall(); - LicenseContent licenseContent = licenseManager - .install(new File(getLicensePath() + "clientLicense/license.lic")); + LicenseContent licenseContent = licenseManager.install(new File(properties + .getStorePath() + File.separator + "clientLicense/license.lic")); log.info("证书认证通过,安装成功"); } catch (Exception e) { e.printStackTrace(); @@ -92,13 +75,4 @@ public class LicenseInstallerBean { throw new VerifyException("证书认证失败:licenseManager is null"); } - /** - * 获取license文件位置 - * - * @return - */ - private String getLicensePath() { - return licensePath; - } - } diff --git a/continew-starter-license/continew-starter-license-verify/src/main/java/top/continew/license/config/LicenseAutoConfiguration.java b/continew-starter-license/continew-starter-license-verify/src/main/java/top/continew/license/config/LicenseAutoConfiguration.java deleted file mode 100644 index cc15d6af..00000000 --- a/continew-starter-license/continew-starter-license-verify/src/main/java/top/continew/license/config/LicenseAutoConfiguration.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. - *

- * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * http://www.gnu.org/licenses/lgpl.html - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package top.continew.license.config; - -import de.schlichtherle.license.*; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import top.continew.license.bean.LicenseInstallerBean; -import top.continew.license.manager.CustomLicenseManager; - -/** - * @Desc: - * @Author loach - * @ClassName top.continew.license.config.LicenseAutoConfiguration - * @Date 2025-04-15 15:17 - */ -@Configuration -public class LicenseAutoConfiguration { - - private String licensePath; - - @Bean - public LicenseVerifyProperties licenseVerifyProperties() { - return new LicenseVerifyProperties(); - } - - @Bean - public LicenseInstallerBean licenseInstallerBean(LicenseVerifyProperties properties) { - return new LicenseInstallerBean(properties); - } - - @Bean - public LicenseManager licenseManager(LicenseVerifyProperties properties) { - return CustomLicenseManager.getInstance(properties); - } - -} diff --git a/continew-starter-license/continew-starter-license-verify/src/main/java/top/continew/license/manager/CustomLicenseManager.java b/continew-starter-license/continew-starter-license-verify/src/main/java/top/continew/license/manager/CustomLicenseManager.java index 223f6cf6..e3905e7a 100644 --- a/continew-starter-license/continew-starter-license-verify/src/main/java/top/continew/license/manager/CustomLicenseManager.java +++ b/continew-starter-license/continew-starter-license-verify/src/main/java/top/continew/license/manager/CustomLicenseManager.java @@ -25,7 +25,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import top.continew.license.bean.LicenseInstallerBean; -import top.continew.license.config.LicenseVerifyProperties; +import top.continew.license.autoconfigure.LicenseVerifyProperties; import top.continew.license.dto.ConfigParam; import top.continew.license.dto.LicenseExtraModel; import top.continew.license.keyStoreParam.CustomKeyStoreParam; @@ -37,10 +37,8 @@ import java.util.prefs.Preferences; /** * 客户端证书管理类(证书验证) * - * @Desc: - * @Author loach - * @ClassName top.continew.license.manager.ClientLicenseManager - * @Date 2025-04-11 15:00 + * @author loach + * @since 1.2.0 */ @Component public class CustomLicenseManager extends LicenseManager { @@ -50,6 +48,28 @@ public class CustomLicenseManager extends LicenseManager { private static volatile CustomLicenseManager INSTANCE; private LicenseExtraModel extraModel; + private LicenseVerifyProperties properties; + + private CustomLicenseManager(LicenseVerifyProperties properties) { + this.properties = properties; + // 初始化服务信息 + initServerExtraModel(); + // 解压证书和配置文件等 + extractZip(); + // 获取配置文件 + ConfigParam configParam = getConfigParam(); + // 安装证书 + Preferences preferences = Preferences.userNodeForPackage(LicenseInstallerBean.class); + CipherParam cipherParam = new DefaultCipherParam(configParam.getStorePass()); + KeyStoreParam publicKeyStoreParam = new CustomKeyStoreParam(LicenseInstallerBean.class, properties + .getStorePath() + File.separator + "clientLicense/publicCerts.keystore", configParam + .getPublicAlias(), configParam.getStorePass(), null); + LicenseParam licenseParam = new DefaultLicenseParam(configParam + .getSubject(), preferences, publicKeyStoreParam, cipherParam); + + super.setLicenseParam(licenseParam); + } + public static CustomLicenseManager getInstance(LicenseVerifyProperties properties) { if (INSTANCE == null) { synchronized (CustomLicenseManager.class) { @@ -61,36 +81,6 @@ public class CustomLicenseManager extends LicenseManager { return INSTANCE; } - private String licensePath; - - public CustomLicenseManager(LicenseVerifyProperties properties) { - if (properties == null || properties.getSavePath() == null) { - String os = System.getProperty("os.name"); - if (os.toLowerCase().contains("windows")) { - this.licensePath = "D:/license/"; - } - this.licensePath = "/data/license/"; - } else { - this.licensePath = properties.getSavePath(); - - } - //初始化服务信息 - initServerExtraModel(); - //解压证书和配置文件等 - extractZip(); - //获取配置文件 - ConfigParam configParam = getConfigParam(); - //安装证书 - Preferences preferences = Preferences.userNodeForPackage(LicenseInstallerBean.class); - CipherParam cipherParam = new DefaultCipherParam(configParam.getStorePass()); - KeyStoreParam publicKeyStoreParam = new CustomKeyStoreParam(LicenseInstallerBean.class, getLicensePath() + "clientLicense/publicCerts.keystore", configParam - .getPublicAlias(), configParam.getStorePass(), null); - LicenseParam licenseParam = new DefaultLicenseParam(configParam - .getSubject(), preferences, publicKeyStoreParam, cipherParam); - - super.setLicenseParam(licenseParam); - } - private void initServerExtraModel() { this.extraModel = ServerInfoUtils.getServerInfos(); } @@ -101,8 +91,8 @@ public class CustomLicenseManager extends LicenseManager { * @throws ZipException */ private void extractZip() { - ZipFile config = new ZipFile(getLicensePath() + "clientLicense.zip"); - File licenseDir = new File(getLicensePath() + "clientLicense"); + ZipFile config = new ZipFile(properties.getStorePath() + File.separator + "clientLicense.zip"); + File licenseDir = new File(properties.getStorePath() + File.separator + "clientLicense"); if (!licenseDir.exists()) { licenseDir.mkdir(); } @@ -124,7 +114,8 @@ public class CustomLicenseManager extends LicenseManager { FileInputStream config = null; BufferedReader reader = null; try { - config = new FileInputStream(getLicensePath() + "clientLicense/clientConfig.json"); + config = new FileInputStream(properties + .getStorePath() + File.separator + "clientLicense/clientConfig.json"); reader = new BufferedReader(new InputStreamReader(config, "UTF-8")); StringBuilder sb = new StringBuilder(); String temp = null; @@ -155,23 +146,14 @@ public class CustomLicenseManager extends LicenseManager { return null; } - /** - * 获取license文件位置 - * - * @return - */ - private String getLicensePath() { - return this.licensePath; - } - /** * 重写验证证书方法,添加自定义参数验证 */ @Override protected synchronized void validate(LicenseContent content) throws LicenseContentException { - //系统验证基本参数:生效时间、失效时间、公钥别名、公钥密码 + // 系统验证基本参数:生效时间、失效时间、公钥别名、公钥密码 super.validate(content); - //验证自定义参数 + // 验证自定义参数 Object o = content.getExtra(); if (o != null && extraModel != null && o instanceof LicenseExtraModel) { LicenseExtraModel contentExtraModel = (LicenseExtraModel)o; diff --git a/continew-starter-license/continew-starter-license-verify/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/continew-starter-license/continew-starter-license-verify/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports index 8c8cb1f9..5ea5a597 100644 --- a/continew-starter-license/continew-starter-license-verify/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports +++ b/continew-starter-license/continew-starter-license-verify/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -1,2 +1 @@ -top.continew.license.config.LicenseAutoConfiguration -top.continew.license.Initializing.LicenseStarterInitializingBean \ No newline at end of file +top.continew.license.autoconfigure.LicenseVerifyAutoConfiguration \ No newline at end of file diff --git a/continew-starter-license/continew-starter-license-verify/src/main/resources/default-license.yml b/continew-starter-license/continew-starter-license-verify/src/main/resources/default-license.yml deleted file mode 100644 index c14b1635..00000000 --- a/continew-starter-license/continew-starter-license-verify/src/main/resources/default-license.yml +++ /dev/null @@ -1,2 +0,0 @@ -license: - savePath: C:/license/ \ No newline at end of file diff --git a/continew-starter-license/pom.xml b/continew-starter-license/pom.xml index 7aedb12a..3c43369c 100644 --- a/continew-starter-license/pom.xml +++ b/continew-starter-license/pom.xml @@ -11,11 +11,17 @@ continew-starter-license pom - ContiNew Starter license管理模块 + ContiNew Starter license 管理模块 continew-starter-license-generate continew-starter-license-verify - + + + + top.continew + continew-starter-core + + \ No newline at end of file