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

This commit is contained in:
贾同学
2025-10-15 14:27:36 +08:00
parent d4e09a09cf
commit 2f75fe062b
2 changed files with 15 additions and 15 deletions

View File

@@ -8,7 +8,7 @@ import java.io.File;
@Data @Data
@ConfigurationProperties(prefix = "activate") @ConfigurationProperties(prefix = "activate")
public class ActiveProperties { public class ActivateProperties {
private final String LICENSE_FILE_NAME = "license.key"; private final String LICENSE_FILE_NAME = "license.key";
/** /**

View File

@@ -7,7 +7,7 @@ import cn.hutool.json.JSONUtil;
import com.njcn.common.pojo.enums.response.CommonResponseEnum; import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.exception.BusinessException; import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.common.utils.RSAUtil; import com.njcn.common.utils.RSAUtil;
import com.njcn.gather.tool.active.config.ActiveProperties; import com.njcn.gather.tool.active.config.ActivateProperties;
import com.njcn.gather.tool.active.service.ActivateService; import com.njcn.gather.tool.active.service.ActivateService;
import com.njcn.gather.tool.active.vo.ActivationCodePlaintext; import com.njcn.gather.tool.active.vo.ActivationCodePlaintext;
import com.njcn.gather.tool.active.vo.ActivationModule; import com.njcn.gather.tool.active.vo.ActivationModule;
@@ -17,13 +17,13 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@EnableConfigurationProperties(ActiveProperties.class) @EnableConfigurationProperties(ActivateProperties.class)
@RequiredArgsConstructor @RequiredArgsConstructor
@Slf4j @Slf4j
@Service @Service
public class ActivateServiceImpl implements ActivateService { public class ActivateServiceImpl implements ActivateService {
private final ActiveProperties rsaProperties; private final ActivateProperties props;
@Override @Override
public String generateApplicationCode(ApplicationCodePlaintext data) { public String generateApplicationCode(ApplicationCodePlaintext data) {
@@ -34,7 +34,7 @@ public class ActivateServiceImpl implements ActivateService {
String plaintext = JSONUtil.toJsonStr(data); String plaintext = JSONUtil.toJsonStr(data);
// RSA 加密 // RSA 加密
try { try {
return RSAUtil.encrypt(plaintext, RSAUtil.stringToPublicKey(rsaProperties.getPublicKey())); return RSAUtil.encrypt(plaintext, RSAUtil.stringToPublicKey(props.getPublicKey()));
} catch (Exception e) { } catch (Exception e) {
log.error("申请码加密失败", e); log.error("申请码加密失败", e);
throw new BusinessException(CommonResponseEnum.FAIL, "申请码生成失败"); throw new BusinessException(CommonResponseEnum.FAIL, "申请码生成失败");
@@ -45,7 +45,7 @@ public class ActivateServiceImpl implements ActivateService {
public ActivationCodePlaintext verifyActivationCode(String activationCode) { public ActivationCodePlaintext verifyActivationCode(String activationCode) {
String plaintext; String plaintext;
try { try {
plaintext = RSAUtil.decrypt(activationCode, RSAUtil.stringToPrivateKey(rsaProperties.getPrivateKey())); plaintext = RSAUtil.decrypt(activationCode, RSAUtil.stringToPrivateKey(props.getPrivateKey()));
} catch (Exception e) { } catch (Exception e) {
log.error("授权码解密失败", e); log.error("授权码解密失败", e);
throw new BusinessException(CommonResponseEnum.FAIL, "无效的激活码"); throw new BusinessException(CommonResponseEnum.FAIL, "无效的激活码");
@@ -62,13 +62,13 @@ public class ActivateServiceImpl implements ActivateService {
@Override @Override
public ActivationCodePlaintext readLicenseFile() { public ActivationCodePlaintext readLicenseFile() {
String licenseFilePath = rsaProperties.getLicenseFilePath(); String licenseFilePath = props.getLicenseFilePath();
log.info("读取授权文件,{}", licenseFilePath); log.info("读取授权文件,{}", licenseFilePath);
if (FileUtil.exist(licenseFilePath)) { if (FileUtil.exist(licenseFilePath)) {
String content = FileUtil.readUtf8String(licenseFilePath); String content = FileUtil.readUtf8String(licenseFilePath);
String plaintext; String plaintext;
try { try {
plaintext = RSAUtil.decrypt(content, RSAUtil.stringToPrivateKey(rsaProperties.getPrivateKey())); plaintext = RSAUtil.decrypt(content, RSAUtil.stringToPrivateKey(props.getPrivateKey()));
} catch (Exception e) { } catch (Exception e) {
log.error("授权文件内容解密失败", e); log.error("授权文件内容解密失败", e);
throw new BusinessException(CommonResponseEnum.FAIL, "许可信息读取失败"); throw new BusinessException(CommonResponseEnum.FAIL, "许可信息读取失败");
@@ -83,7 +83,7 @@ public class ActivateServiceImpl implements ActivateService {
// RSA 解密 // RSA 解密
String plaintext; String plaintext;
try { try {
plaintext = RSAUtil.decrypt(applicationCode, RSAUtil.stringToPrivateKey(rsaProperties.getPrivateKey())); plaintext = RSAUtil.decrypt(applicationCode, RSAUtil.stringToPrivateKey(props.getPrivateKey()));
} catch (Exception e) { } catch (Exception e) {
log.error("申请码解密失败", e); log.error("申请码解密失败", e);
throw new BusinessException(CommonResponseEnum.FAIL, "无效的申请码"); throw new BusinessException(CommonResponseEnum.FAIL, "无效的申请码");
@@ -113,7 +113,7 @@ public class ActivateServiceImpl implements ActivateService {
String jsonStr = JSONUtil.toJsonStr(activationCodePlaintext); String jsonStr = JSONUtil.toJsonStr(activationCodePlaintext);
log.info("生成激活码明文:{}", jsonStr); log.info("生成激活码明文:{}", jsonStr);
try { try {
return RSAUtil.encrypt(jsonStr, RSAUtil.stringToPublicKey(rsaProperties.getPublicKey())); return RSAUtil.encrypt(jsonStr, RSAUtil.stringToPublicKey(props.getPublicKey()));
} catch (Exception e) { } catch (Exception e) {
log.error("生成激活码失败", e); log.error("生成激活码失败", e);
throw new BusinessException(CommonResponseEnum.FAIL, "生成激活码失败"); throw new BusinessException(CommonResponseEnum.FAIL, "生成激活码失败");
@@ -123,9 +123,9 @@ public class ActivateServiceImpl implements ActivateService {
/** /**
* 添加或更新授权文件 * 添加或更新授权文件
* *
* @param newActivationCodePlaintext * @param newActivationCodePlaintext 新授权码明文
* @param activationCode * @param activationCode 授权码
* @return * @return 最新授权码明文
*/ */
private ActivationCodePlaintext addOrUpdateLicenseFile(ActivationCodePlaintext newActivationCodePlaintext, String activationCode) { private ActivationCodePlaintext addOrUpdateLicenseFile(ActivationCodePlaintext newActivationCodePlaintext, String activationCode) {
@@ -147,7 +147,7 @@ public class ActivateServiceImpl implements ActivateService {
log.error("授权码格式错误"); log.error("授权码格式错误");
throw new BusinessException(CommonResponseEnum.FAIL, "无效的激活码"); throw new BusinessException(CommonResponseEnum.FAIL, "无效的激活码");
} }
String licenseFilePath = rsaProperties.getLicenseFilePath(); String licenseFilePath = props.getLicenseFilePath();
log.info("授权文件路径:{}", licenseFilePath); log.info("授权文件路径:{}", licenseFilePath);
ActivationCodePlaintext oldActivationCodePlaintext = this.readLicenseFile(); ActivationCodePlaintext oldActivationCodePlaintext = this.readLicenseFile();
if (oldActivationCodePlaintext == null) { if (oldActivationCodePlaintext == null) {
@@ -172,7 +172,7 @@ public class ActivateServiceImpl implements ActivateService {
String updateContent; String updateContent;
// 重新加密 // 重新加密
try { try {
updateContent = RSAUtil.encrypt(JSONUtil.toJsonStr(oldActivationCodePlaintext), RSAUtil.stringToPublicKey(rsaProperties.getPublicKey())); updateContent = RSAUtil.encrypt(JSONUtil.toJsonStr(oldActivationCodePlaintext), RSAUtil.stringToPublicKey(props.getPublicKey()));
} catch (Exception e) { } catch (Exception e) {
log.error("授权文件内容加密失败", e); log.error("授权文件内容加密失败", e);
throw new BusinessException(CommonResponseEnum.FAIL, "激活失败,请联系管理员"); throw new BusinessException(CommonResponseEnum.FAIL, "激活失败,请联系管理员");