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

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