refactor(license): 优化 License 模块部分代码

This commit is contained in:
2025-04-29 22:31:36 +08:00
parent 06f5a0f346
commit 7d97026480
35 changed files with 350 additions and 473 deletions

View File

@@ -7,27 +7,27 @@
<artifactId>continew-starter-license</artifactId>
<version>${revision}</version>
</parent>
<artifactId>continew-starter-license-core</artifactId>
<description>ContiNew Starter License核心模块</description>
<dependencies>
<!-- continew starter 核心模块 -->
<artifactId>continew-starter-license-core</artifactId>
<description>ContiNew Starter License 模块 - 核心模块</description>
<dependencies>
<!-- 核心模块 -->
<dependency>
<groupId>top.continew</groupId>
<artifactId>continew-starter-core</artifactId>
</dependency>
<!-- license 依赖-->
<!-- TrueLicense (一个开源的证书管理引擎)-->
<dependency>
<groupId>de.schlichtherle.truelicense</groupId>
<artifactId>truelicense-core</artifactId>
</dependency>
<!--zip4j压缩文件-->
<!-- Zip4j (开源的 Java 处理 zip 压缩文件的开发包) -->
<dependency>
<groupId>net.lingala.zip4j</groupId>
<artifactId>zip4j</artifactId>
</dependency>
</dependencies>
</project>

View File

@@ -26,7 +26,7 @@ import java.io.Serializable;
*
* @author echo
* @since 2.11.0
**/
*/
public class BuildCreatorResp implements Serializable {
@Serial
@@ -38,7 +38,7 @@ public class BuildCreatorResp implements Serializable {
private LicenseCreatorParam param;
/**
* 客户端zip文件
* 客户端 Zip 文件
*/
private ZipFile clientZipFile;

View File

@@ -21,7 +21,7 @@ package top.continew.license.model;
*
* @author loach
* @since 2.11.0
**/
*/
public class ConfigParam {
/**

View File

@@ -30,6 +30,7 @@ import java.io.InputStream;
* @since 2.11.0
*/
public class CustomKeyStoreParam extends AbstractKeyStoreParam {
/**
* 密钥路径,可为磁盘路径,也可为项目资源文件里的路径,如果为磁盘路径需重写getStream()方法
*/
@@ -50,8 +51,8 @@ public class CustomKeyStoreParam extends AbstractKeyStoreParam {
*/
private String keyPass;
public CustomKeyStoreParam(Class aClass, String s) {
super(aClass, s);
public CustomKeyStoreParam(Class clazz, String s) {
super(clazz, s);
}
public CustomKeyStoreParam(Class clazz, String resource, String alias, String storePass, String keyPass) {

View File

@@ -25,7 +25,7 @@ import java.util.Date;
*
* @author loach
* @since 2.11.0
**/
*/
public class LicenseCreatorParam implements Serializable {
@Serial

View File

@@ -23,7 +23,7 @@ import java.util.Date;
*
* @author loach
* @since 2.11.0
**/
*/
public class LicenseCreatorParamVO {
/**

View File

@@ -23,7 +23,7 @@ import java.util.Set;
*
* @author loach
* @since 2.11.0
**/
*/
public class LicenseExtraModel {
/**

View File

@@ -28,11 +28,14 @@ import java.io.LineNumberReader;
*
* @author loach
* @since 2.11.0
**/
*/
public class ExecCmdUtil {
private static final String CREATE_3RDSESSION_SHELL_SCRIPT = "head -n 80 /dev/urandom | tr -dc A-Za-z0-9 | head -c 168";
private ExecCmdUtil() {
}
/**
* 执行cmd命令(shell脚本)
*
@@ -82,7 +85,6 @@ public class ExecCmdUtil {
* 执行linux命令(shell脚本)生成3rd_session随机数
*/
public static String create3rdSessionToken() throws IOException {
return exec(CREATE_3RDSESSION_SHELL_SCRIPT);
}
}

View File

@@ -52,6 +52,9 @@ public class ServerInfoUtils {
private static String mainBoardSerial = null;
}
private ServerInfoUtils() {
}
/**
* 组装需要额外校验的License参数
*
@@ -324,7 +327,6 @@ public class ServerInfoUtils {
* @throws Exception 默认异常
*/
private static Set<InetAddress> getLocalAllInetAddress() throws Exception {
Set<InetAddress> result = CollUtil.newHashSet();
// 遍历所有的网络接口
for (Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
@@ -342,5 +344,4 @@ public class ServerInfoUtils {
}
return result;
}
}

View File

@@ -1,82 +0,0 @@
## continew-starter-license-generate 使用方法
1. 引入依赖
```java
<dependency>
<groupId>top.continew</groupId>
<artifactId>continew-starter-license-generate</artifactId>
<version>2.11.0-SNAPSHOT</version>
</dependency>
```
2. 开发对外接口
```
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.admin.controller.license;
import cn.dev33.satoken.annotation.SaIgnore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.continew.license.model.LicenseCreatorParamVO;
import top.continew.license.model.LicenseExtraModel;
import top.continew.license.service.LicenseCreateService;
import java.util.Calendar;
import java.util.Date;
/**
* @Desc:
* @Author loach
* @ClassName top.continew.admin.controller.license.LicenseGenerateController
* @Date 2025-04-15 10:52
*/
@RestController
@RequestMapping("/license")
public class LicenseGenerateController {
@Autowired
private LicenseCreateService licenseCreateService;
@GetMapping("/generate")
public void listDict() throws Exception {
//设置证书校验参数
LicenseCreatorParamVO paramVO = new LicenseCreatorParamVO();
paramVO.setCustomerName("continew");
paramVO.setDescription("continew");
paramVO.setKeyPass("123456a");
paramVO.setStorePass("123456a");
//设置过期时间
Calendar calendar = Calendar.getInstance();
long expire = Instant.now().toEpochMilli() + (24L * 3600L * 1000L);;
// long expire = new Date().getTime() + (24L * 3600L * 1000L);
calendar.setTimeInMillis(expire);
paramVO.setExpireTime(calendar.getTime());
//设置额外校验参数(服务器信息)
LicenseExtraModel extraModel = licenseCreateService.getServerInfo();
paramVO.setLicenseExtraModel(extraModel);
licenseCreateService.generateLicense(paramVO);
}
}
```
注:默认生成 license 为FileUtil.getTmpDirPath()下。将压缩包发送给客户端使用。

View File

@@ -9,16 +9,14 @@
<version>${revision}</version>
</parent>
<artifactId>continew-starter-license-verify</artifactId>
<packaging>jar</packaging>
<description>ContiNew Starter License校验模块</description>
<artifactId>continew-starter-license-generator</artifactId>
<description>ContiNew Starter License 模块 - 生成器</description>
<dependencies>
<!-- license 核心模块 -->
<!-- License 模块 - 核心模块 -->
<dependency>
<groupId>top.continew</groupId>
<artifactId>continew-starter-license-core</artifactId>
</dependency>
</dependencies>
</project>

View File

@@ -36,7 +36,7 @@ import top.continew.starter.core.constant.PropertiesConstants;
*/
@AutoConfiguration
@EnableConfigurationProperties(LicenseGenerateProperties.class)
@ConditionalOnProperty(prefix = PropertiesConstants.LICENSE_GENERATE, name = PropertiesConstants.ENABLED, havingValue = "true", matchIfMissing = true)
@ConditionalOnProperty(prefix = PropertiesConstants.LICENSE_GENERATOR, name = PropertiesConstants.ENABLED, havingValue = "true", matchIfMissing = true)
public class LicenseGenerateAutoConfiguration {
private static final Logger log = LoggerFactory.getLogger(LicenseGenerateAutoConfiguration.class);
@@ -52,6 +52,6 @@ public class LicenseGenerateAutoConfiguration {
@PostConstruct
public void postConstruct() {
log.debug("[ContiNew Starter] - Auto Configuration 'License-Generate' completed initialization.");
log.debug("[ContiNew Starter] - Auto Configuration 'License-Generator' completed initialization.");
}
}

View File

@@ -25,8 +25,9 @@ import top.continew.starter.core.constant.PropertiesConstants;
* @author Jasmine
* @since 2.11.0
*/
@ConfigurationProperties(PropertiesConstants.LICENSE_GENERATE)
@ConfigurationProperties(PropertiesConstants.LICENSE_GENERATOR)
public class LicenseGenerateProperties {
/**
* 是否启用
*/

View File

@@ -43,8 +43,7 @@ import java.util.prefs.Preferences;
*
* @author loach
* @since 2.11.0
**/
*/
public class LicenseCreateService {
private static final Logger log = LoggerFactory.getLogger(LicenseCreateService.class);
@@ -88,7 +87,6 @@ public class LicenseCreateService {
* @throws Exception 例外
*/
public void generateLicense(LicenseCreatorParamVO paramVO) throws Exception {
BuildCreatorResp buildCreatorResp = buildCreator(paramVO);
LicenseCreatorParam param = buildCreatorResp.getParam();
ZipFile clientZipFile = buildCreatorResp.getClientZipFile();
@@ -186,7 +184,6 @@ public class LicenseCreateService {
private ZipFile generateClientConfig(LicenseCreatorParam param,
String currentCustomerDir,
String publicAlias) throws Exception {
ZipFile clientLicense = new ZipFile(currentCustomerDir + "clientLicense.zip");
File config = new File(currentCustomerDir + "clientConfig.json");
ConfigParam configParam = new ConfigParam();
@@ -254,7 +251,6 @@ public class LicenseCreateService {
* @return {@link String }
*/
private String relativePath(LicenseCreatorParamVO paramVO) {
if (paramVO.getLicenseSavePath() != null) {
return paramVO.getLicenseSavePath();
}
@@ -284,7 +280,6 @@ public class LicenseCreateService {
* @return {@link LicenseContent }
*/
private LicenseContent initLicenseContent(LicenseCreatorParam param) {
LicenseContent licenseContent = new LicenseContent();
licenseContent.setHolder(DEFAULT_HOLDER_ISSUER);
licenseContent.setIssuer(DEFAULT_HOLDER_ISSUER);

View File

@@ -9,12 +9,11 @@
<version>${revision}</version>
</parent>
<artifactId>continew-starter-license-generate</artifactId>
<packaging>jar</packaging>
<description>ContiNew Starter License生成模块</description>
<artifactId>continew-starter-license-verifier</artifactId>
<description>ContiNew Starter License 模块 - 校验器</description>
<dependencies>
<!-- license 核心模块 -->
<!-- License 模块 - 核心模块 -->
<dependency>
<groupId>top.continew</groupId>
<artifactId>continew-starter-license-core</artifactId>

View File

@@ -26,7 +26,7 @@ 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.initializing.LicenseStarterInitializingBean;
import top.continew.license.bean.LicenseInstallerBean;
import top.continew.license.manager.CustomLicenseManager;
import top.continew.starter.core.constant.PropertiesConstants;
@@ -39,7 +39,7 @@ import top.continew.starter.core.constant.PropertiesConstants;
*/
@AutoConfiguration
@EnableConfigurationProperties(LicenseVerifyProperties.class)
@ConditionalOnProperty(prefix = PropertiesConstants.LICENSE_VERIFY, name = PropertiesConstants.ENABLED, havingValue = "true", matchIfMissing = true)
@ConditionalOnProperty(prefix = PropertiesConstants.LICENSE_VERIFIER, name = PropertiesConstants.ENABLED, havingValue = "true", matchIfMissing = true)
public class LicenseVerifyAutoConfiguration {
private static final Logger log = LoggerFactory.getLogger(LicenseVerifyAutoConfiguration.class);
@@ -80,7 +80,7 @@ public class LicenseVerifyAutoConfiguration {
@PostConstruct
public void postConstruct() {
log.debug("[ContiNew Starter] - Auto Configuration 'License-Verify' completed initialization.");
log.debug("[ContiNew Starter] - Auto Configuration 'License-Verifier' completed initialization.");
}
}

View File

@@ -26,8 +26,8 @@ import top.continew.starter.core.constant.PropertiesConstants;
*
* @author loach
* @since 2.11.0
**/
@ConfigurationProperties(PropertiesConstants.LICENSE_VERIFY)
*/
@ConfigurationProperties(PropertiesConstants.LICENSE_VERIFIER)
public class LicenseVerifyProperties {
/**

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package top.continew.license.Initializing;
package top.continew.license.initializing;
import org.springframework.beans.factory.InitializingBean;
import top.continew.license.bean.LicenseInstallerBean;

View File

@@ -1,22 +0,0 @@
## continew-starter-license-verify 食用方法
1. 引入依赖
```java
<dependency>
<groupId>top.continew</groupId>
<artifactId>continew-starter-license-verify</artifactId>
<version>2.11.0-SNAPSHOT</version>
</dependency>
```
2. 配置YMLlicense 压缩包存放位置)
```yaml
continew-starter:
license:
verify:
storePath: D:/license
```
注:默认加载 `FileUtil.getTmpDirPath()` 位置。

View File

@@ -12,9 +12,10 @@
<artifactId>continew-starter-license</artifactId>
<packaging>pom</packaging>
<description>ContiNew Starter License模块</description>
<modules>
<module>continew-starter-license-core</module>
<module>continew-starter-license-generate</module>
<module>continew-starter-license-verify</module>
<module>continew-starter-license-generator</module>
<module>continew-starter-license-verifier</module>
</modules>
</project>