refactor(license): 优化 license 模块

This commit is contained in:
jasmine
2025-04-17 06:29:34 +00:00
committed by Charles7c
parent da4c8154bf
commit 1ce5c023cf
22 changed files with 345 additions and 306 deletions

View File

@@ -13,10 +13,10 @@
2. 配置YMLlicense 压缩包存放位置)
```yaml
license:
savePath: D:/license/
continew-starter:
license:
verify:
storePath: D:/license
```
注:默认加载 `D:/license/` 位置。
注:默认加载 `FileUtil.getTmpDirPath()` 位置。

View File

@@ -13,54 +13,23 @@
<packaging>jar</packaging>
<description>license 校验模块 基于 truelicens 实现</description>
<properties>
<truelicense.version>1.33</truelicense.version>
<zip4j.version>2.2.6</zip4j.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<!--ContiNew Starter 日志模块 - 核心模块-->
<dependency>
<groupId>top.continew</groupId>
<artifactId>continew-starter-log-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- license 依赖-->
<dependency>
<groupId>de.schlichtherle.truelicense</groupId>
<artifactId>truelicense-core</artifactId>
<version>${truelicense.version}</version>
</dependency>
<!--zip4j压缩文件-->
<dependency>
<groupId>net.lingala.zip4j</groupId>
<artifactId>zip4j</artifactId>
<version>${zip4j.version}</version>
</dependency>
<!--jackson-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
</dependencies>
</project>

View File

@@ -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();
}
}

View File

@@ -0,0 +1,73 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
* <p>
* 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
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* 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.");
}
}

View File

@@ -14,27 +14,45 @@
* limitations under the License.
*/
package top.continew.license.config;
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;
/**
* @Desc:
* @Author loach
* @ClassName top.continew.license.config.LicenseYmlConfig
* @Date 2025-04-14 14:56
* license 校验模块配置属性
*
* @author loach
* @since 1.2.0
*/
@ConfigurationProperties(prefix = "license")
@ConfigurationProperties(PropertiesConstants.LICENSE_VERIFY)
public class LicenseVerifyProperties {
private String savePath;
/**
* 是否启用
*/
private boolean enabled = true;
public String getSavePath() {
return savePath;
/**
* 生成的license文件所在路径
*/
private String storePath = FileUtil.getTmpDirPath();
public boolean isEnabled() {
return enabled;
}
public void setSavePath(String savePath) {
this.savePath = savePath;
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public String getStorePath() {
return storePath;
}
public void setStorePath(String storePath) {
this.storePath = storePath;
}
}

View File

@@ -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;
}
}

View File

@@ -1,51 +0,0 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
* <p>
* 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
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* 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);
}
}

View File

@@ -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;

View File

@@ -1,2 +1 @@
top.continew.license.config.LicenseAutoConfiguration
top.continew.license.Initializing.LicenseStarterInitializingBean
top.continew.license.autoconfigure.LicenseVerifyAutoConfiguration