UPDATE: 优化程序激活码工具。

This commit is contained in:
贾同学
2025-10-15 14:25:39 +08:00
parent d58452012d
commit d4e09a09cf
5 changed files with 67 additions and 32 deletions

View File

@@ -118,7 +118,7 @@ power-quality:
harmonic-times: 50 # 谐波次数
ib-add: false # 电流基波叠加标志
uharm-add: false # 电压谐波叠加标志
# rsa公私钥
rsa:
# 激活配置
activate:
private-key: "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCcUyYhVqczGxblL+o/xZzF/8nf+LjrfUE/dS1aRHM7uMDD0cgCArhjtfneFePrMxt+Z7W8yNBzSarub8qsfhaVNikV7Es7oaeTygfjQXTi2n4AFkir3fM07J08RpWhl5M8f8uWTCuvFUYAw00gq55typqmnbkmJa2VIUy/iQf+cMCP7abz4/jNhUzUR3qA7TV4oMRgTdIEDUp63YF8dOC+JH8XxYrCVeHXV6fLCwmesdMzl0lB2VTEKMfLbXhOmF5g7P9y/16VCcN8UBuZlbyYfn+GAxJOSbeHi5HshOKfoSuD7Jz+3WQZpNavOWjIFExKIU38/CvnJCOP7XBCqpSTAgMBAAECggEAYeWokWRE3TpvwiOZnUpR/aVMdVi75a3ROL5XIpqPV61B+t/bU3cEpl0GF9C5pUeiRi0IoStZb3mI9D1KPW/REKyUWkhabQO1gFYbTnRlkNOn6MILzKX4cwJjDaZeeo4EBPU7N+qHyOOXrU6hdH5FfxhMdV983ajm5eeuupxER1C2kAcIklTeVpTX6EKOgZb5LBp5ssOVm2P42pOauvcRozRcvZmqnErXmukv0H4l3EVNt4rHpTn9riHUC63e8JfiYzVaF6zuNUxv6nHEft0/SRMw11XSTnNfDzcKqgjz6ksFBS/6eQQYKESk+ONC53HUuYHFAknkwsPupDCT2W8FIQKBgQDLHT/xCU3nxGr4vFKBDNaO2D5oK20ECbBO4oDvLWWmQG7f+6TsMy8PgVdMnoL4RfqGlwFAKEpS6KVFHnBVqnNEhcdy9uCI7x7Xx8UnyUtxj1EDTm76uta9Ki9OrlqB6tIm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3h4uR7ITin6Erg+yc/t1kGaTWrzloyBRMSiFN/Pwr5yQjj+1wQqqUkwIDAQAB"

View File

@@ -0,0 +1,33 @@
package com.njcn.gather.tool.active.config;
import cn.hutool.system.SystemUtil;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import java.io.File;
@Data
@ConfigurationProperties(prefix = "activate")
public class ActiveProperties {
private final String LICENSE_FILE_NAME = "license.key";
/**
* RSA公钥
*/
private String publicKey;
/**
* RSA私钥
*/
private String privateKey;
/**
* 密钥文件所在目录,默认为当前项目目录
*/
private String licenseDir = System.getProperty(SystemUtil.USER_DIR);
public String getLicenseFilePath() {
return licenseDir + File.separator + LICENSE_FILE_NAME;
}
}

View File

@@ -1,17 +0,0 @@
package com.njcn.gather.tool.active.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
@Data
@ConfigurationProperties(prefix = "rsa")
public class RSAProperties {
/**
* RSA公钥
*/
private String publicKey;
/**
* RSA私钥
*/
private String privateKey;
}

View File

@@ -1,5 +1,7 @@
package com.njcn.gather.tool.active.controller;
import cn.hutool.core.net.NetUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.njcn.common.pojo.annotation.OperateInfo;
import com.njcn.common.pojo.enums.common.LogEnum;
@@ -58,10 +60,26 @@ public class ActivateController extends BaseController {
String methodDescribe = getMethodDescribe("checkLicense");
LogUtil.njcnDebug(log, "{},获取许可信息", methodDescribe);
ActivationCodePlaintext activationCodePlaintext = activateService.readLicenseFile();
String macAddress = NetUtil.getLocalMacAddress();
if (activationCodePlaintext == null) {
activationCodePlaintext = new ActivationCodePlaintext();
activationCodePlaintext.setMacAddress(macAddress);
activationCodePlaintext.init();
} else {
// 校验mac地址
String licenseMacAddress = activationCodePlaintext.getMacAddress();
if (StrUtil.isNotEmpty(licenseMacAddress)) {
if (!StrUtil.equals(licenseMacAddress, macAddress)) {
log.error("mac地址不匹配无效的许可文件本机mac:{},许可mac:{}", macAddress, licenseMacAddress);
methodDescribe = "mac地址不匹配无效的许可文件";
activationCodePlaintext = new ActivationCodePlaintext();
activationCodePlaintext.setMacAddress(macAddress);
activationCodePlaintext.init();
}
}
}
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, activationCodePlaintext, methodDescribe);
}

View File

@@ -7,7 +7,7 @@ import cn.hutool.json.JSONUtil;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.common.utils.RSAUtil;
import com.njcn.gather.tool.active.config.RSAProperties;
import com.njcn.gather.tool.active.config.ActiveProperties;
import com.njcn.gather.tool.active.service.ActivateService;
import com.njcn.gather.tool.active.vo.ActivationCodePlaintext;
import com.njcn.gather.tool.active.vo.ActivationModule;
@@ -17,17 +17,13 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Service;
import java.io.File;
@EnableConfigurationProperties(RSAProperties.class)
@EnableConfigurationProperties(ActiveProperties.class)
@RequiredArgsConstructor
@Slf4j
@Service
public class ActivateServiceImpl implements ActivateService {
private final RSAProperties rsaProperties;
static final String LICENSE_FILE_NAME = "license.key";
static final String LICENSE_FILE_PATH = ActivateServiceImpl.class.getProtectionDomain().getCodeSource().getLocation().getPath() + File.separator + LICENSE_FILE_NAME;
private final ActiveProperties rsaProperties;
@Override
public String generateApplicationCode(ApplicationCodePlaintext data) {
@@ -66,8 +62,10 @@ public class ActivateServiceImpl implements ActivateService {
@Override
public ActivationCodePlaintext readLicenseFile() {
if (FileUtil.exist(LICENSE_FILE_PATH)) {
String content = FileUtil.readUtf8String(LICENSE_FILE_PATH);
String licenseFilePath = rsaProperties.getLicenseFilePath();
log.info("读取授权文件,{}", licenseFilePath);
if (FileUtil.exist(licenseFilePath)) {
String content = FileUtil.readUtf8String(licenseFilePath);
String plaintext;
try {
plaintext = RSAUtil.decrypt(content, RSAUtil.stringToPrivateKey(rsaProperties.getPrivateKey()));
@@ -130,6 +128,7 @@ public class ActivateServiceImpl implements ActivateService {
* @return
*/
private ActivationCodePlaintext addOrUpdateLicenseFile(ActivationCodePlaintext newActivationCodePlaintext, String activationCode) {
log.info("新授权码明文:{}", JSONUtil.toJsonStr(newActivationCodePlaintext));
ActivationModule newContrast = newActivationCodePlaintext.getContrast();
ActivationModule newDigital = newActivationCodePlaintext.getDigital();
@@ -148,16 +147,18 @@ public class ActivateServiceImpl implements ActivateService {
log.error("授权码格式错误");
throw new BusinessException(CommonResponseEnum.FAIL, "无效的激活码");
}
log.info("授权文件路径:{}", LICENSE_FILE_PATH);
String licenseFilePath = rsaProperties.getLicenseFilePath();
log.info("授权文件路径:{}", licenseFilePath);
ActivationCodePlaintext oldActivationCodePlaintext = this.readLicenseFile();
if (oldActivationCodePlaintext == null) {
// 如果文件不存在,创建新文件
FileUtil.touch(LICENSE_FILE_PATH);
FileUtil.touch(licenseFilePath);
// 写入授权文件
FileUtil.writeUtf8String(activationCode, LICENSE_FILE_PATH);
FileUtil.writeUtf8String(activationCode, licenseFilePath);
return newActivationCodePlaintext;
} else {
log.info("旧授权码明文:{}", JSONUtil.toJsonStr(oldActivationCodePlaintext));
oldActivationCodePlaintext.setMacAddress(newActivationCodePlaintext.getMacAddress());
if (newContrast.isApply()) {
oldActivationCodePlaintext.setContrast(newContrast);
}
@@ -176,7 +177,7 @@ public class ActivateServiceImpl implements ActivateService {
log.error("授权文件内容加密失败", e);
throw new BusinessException(CommonResponseEnum.FAIL, "激活失败,请联系管理员");
}
FileUtil.writeUtf8String(updateContent, LICENSE_FILE_PATH);
FileUtil.writeUtf8String(updateContent, licenseFilePath);
return oldActivationCodePlaintext;
}
}