UPDATE: 修改激活逻辑

This commit is contained in:
贾同学
2025-10-22 13:27:24 +08:00
parent b4878d4a25
commit a2de4b80a7
7 changed files with 29 additions and 94 deletions

View File

@@ -10,7 +10,6 @@ import com.njcn.common.pojo.response.HttpResult;
import com.njcn.common.utils.LogUtil;
import com.njcn.gather.tool.active.service.ActivateService;
import com.njcn.gather.tool.active.vo.ActivationCodePlaintext;
import com.njcn.gather.tool.active.vo.ApplicationCodePlaintext;
import com.njcn.web.controller.BaseController;
import com.njcn.web.utils.HttpResultUtil;
import io.swagger.annotations.*;
@@ -33,12 +32,11 @@ public class ActivateController extends BaseController {
@OperateInfo(info = LogEnum.BUSINESS_COMMON)
@ApiOperation("生成设备申请码")
@ApiImplicitParam(name = "data", value = "生成设备申请码参数", required = true)
@PostMapping("/generateApplicationCode")
public HttpResult generateApplicationCode(@RequestBody ApplicationCodePlaintext data) {
public HttpResult generateApplicationCode() {
String methodDescribe = getMethodDescribe("generateApplicationCode");
LogUtil.njcnDebug(log, "{},生成设备申请码{}", methodDescribe, JSONUtil.toJsonStr(data));
String applicationCode = activateService.generateApplicationCode(data);
LogUtil.njcnDebug(log, "{},生成设备申请码", methodDescribe);
String applicationCode = activateService.generateApplicationCode();
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, applicationCode, methodDescribe);
}
@@ -95,7 +93,7 @@ public class ActivateController extends BaseController {
@ApiImplicitParam(name = "params", value = "参数", required = true, dataType = "ApplicationCodeParams")
@PostMapping("/generateActivationCode")
public HttpResult generateActivationCode(@RequestBody ApplicationCodeParams params) {
String activationCode = activateService.generateActivationCode(params.getApplicationCode(), params.getPermanently());
String activationCode = activateService.generateActivationCode(params.getApplicationCode(), params.getActivationModule());
return HttpResultUtil.assembleResult(CommonResponseEnum.SUCCESS.getCode(), activationCode, "");
}
@@ -104,7 +102,7 @@ public class ActivateController extends BaseController {
public static class ApplicationCodeParams {
@ApiModelProperty(value = "设备申请码", required = true)
private String applicationCode;
@ApiModelProperty(value = "是否永久激活", example = "1", required = true)
private int permanently;
@ApiModelProperty(value = "激活模块", required = true)
private ActivationCodePlaintext activationModule;
}
}

View File

@@ -1,7 +1,6 @@
package com.njcn.gather.tool.active.service;
import com.njcn.gather.tool.active.vo.ActivationCodePlaintext;
import com.njcn.gather.tool.active.vo.ApplicationCodePlaintext;
public interface ActivateService {
@@ -9,10 +8,9 @@ public interface ActivateService {
/**
* 生成设备申请码
*
* @param data
* @return
*/
String generateApplicationCode(ApplicationCodePlaintext data);
String generateApplicationCode();
/**
* 验证激活码
@@ -32,11 +30,11 @@ public interface ActivateService {
/**
* 生成设备激活码
*
* @param applicationCode 申请码
* @param permanently 是否永久激活
* @param applicationCode 申请码
* @param activationCodePlaintext
* @return
*/
String generateActivationCode(String applicationCode, int permanently);
String generateActivationCode(String applicationCode, ActivationCodePlaintext activationCodePlaintext);
}

View File

@@ -26,10 +26,11 @@ public class ActivateServiceImpl implements ActivateService {
private final ActivateProperties props;
@Override
public String generateApplicationCode(ApplicationCodePlaintext data) {
public String generateApplicationCode() {
// 获取当前设备MAC地址
String macAddress = NetUtil.getLocalMacAddress();
log.debug("当前设备MAC地址{}", macAddress);
ApplicationCodePlaintext data = new ApplicationCodePlaintext();
data.setMacAddress(macAddress);
String plaintext = JSONUtil.toJsonStr(data);
// RSA 加密
@@ -79,7 +80,7 @@ public class ActivateServiceImpl implements ActivateService {
}
@Override
public String generateActivationCode(String applicationCode, int permanently) {
public String generateActivationCode(String applicationCode, ActivationCodePlaintext activationCodePlaintext) {
// RSA 解密
String plaintext;
try {
@@ -88,27 +89,18 @@ public class ActivateServiceImpl implements ActivateService {
log.error("申请码解密失败", e);
throw new BusinessException(CommonResponseEnum.FAIL, "无效的申请码");
}
ActivationCodePlaintext activationCodePlaintext = JSONUtil.toBean(plaintext, ActivationCodePlaintext.class);
if (activationCodePlaintext == null) {
ApplicationCodePlaintext applicationCodePlaintext = JSONUtil.toBean(plaintext, ApplicationCodePlaintext.class);
if (applicationCodePlaintext == null) {
log.error("申请码内容为空");
throw new BusinessException(CommonResponseEnum.FAIL, "无效的申请码");
}
String macAddress = applicationCodePlaintext.getMacAddress();
if (StrUtil.isBlank(macAddress)) {
log.error("mac地址为空");
throw new BusinessException(CommonResponseEnum.FAIL, "无效的申请码");
}
// 激活码明文
ActivationModule contrast = activationCodePlaintext.getContrast();
ActivationModule simulate = activationCodePlaintext.getSimulate();
ActivationModule digital = activationCodePlaintext.getDigital();
if (contrast.isApply()) {
contrast.setPermanently(permanently);
activationCodePlaintext.setContrast(contrast);
}
if (simulate.isApply()) {
simulate.setPermanently(permanently);
activationCodePlaintext.setSimulate(simulate);
}
if (digital.isApply()) {
digital.setPermanently(permanently);
activationCodePlaintext.setDigital(digital);
}
activationCodePlaintext.setMacAddress(applicationCodePlaintext.getMacAddress());
// RSA 加密
String jsonStr = JSONUtil.toJsonStr(activationCodePlaintext);
log.info("生成激活码明文:{}", jsonStr);
@@ -133,20 +125,7 @@ public class ActivateServiceImpl implements ActivateService {
ActivationModule newContrast = newActivationCodePlaintext.getContrast();
ActivationModule newDigital = newActivationCodePlaintext.getDigital();
ActivationModule newSimulate = newActivationCodePlaintext.getSimulate();
boolean verifyStatus = false;
if (newContrast.isApply() && newContrast.isPermanently()) {
verifyStatus = true;
}
if (newDigital.isApply() && newDigital.isPermanently()) {
verifyStatus = true;
}
if (newSimulate.isApply() && newSimulate.isPermanently()) {
verifyStatus = true;
}
if (!verifyStatus) {
log.error("授权码格式错误");
throw new BusinessException(CommonResponseEnum.FAIL, "无效的激活码");
}
String licenseFilePath = props.getLicenseFilePath();
log.info("授权文件路径:{}", licenseFilePath);
ActivationCodePlaintext oldActivationCodePlaintext = this.readLicenseFile();
@@ -159,13 +138,13 @@ public class ActivateServiceImpl implements ActivateService {
} else {
log.info("旧授权码明文:{}", JSONUtil.toJsonStr(oldActivationCodePlaintext));
oldActivationCodePlaintext.setMacAddress(newActivationCodePlaintext.getMacAddress());
if (newContrast.isApply()) {
if (newContrast != null && newContrast.isPermanently()) {
oldActivationCodePlaintext.setContrast(newContrast);
}
if (newDigital.isApply()) {
if (newDigital != null && newDigital.isPermanently()) {
oldActivationCodePlaintext.setDigital(newDigital);
}
if (newSimulate.isApply()) {
if (newSimulate != null && newSimulate.isPermanently()) {
oldActivationCodePlaintext.setSimulate(newSimulate);
}
log.info("最新授权码明文:{}", JSONUtil.toJsonStr(oldActivationCodePlaintext));

View File

@@ -1,13 +1,11 @@
package com.njcn.gather.tool.active.vo;
import lombok.Data;
import lombok.EqualsAndHashCode;
@EqualsAndHashCode(callSuper = true)
@Data
public class ActivationCodePlaintext {
/**
* mac地址
*/
private String macAddress;
public class ActivationCodePlaintext extends ApplicationCodePlaintext {
/**
* 模拟式模块

View File

@@ -1,11 +1,9 @@
package com.njcn.gather.tool.active.vo;
import lombok.Data;
import lombok.EqualsAndHashCode;
@EqualsAndHashCode(callSuper = true)
@Data
public class ActivationModule extends ApplicationModule {
public class ActivationModule {
/**
* 是否永久授权

View File

@@ -13,23 +13,4 @@ public class ApplicationCodePlaintext {
@ApiModelProperty(value = "mac地址", hidden = true)
private String macAddress;
/**
* 模拟式模块
*/
@ApiModelProperty("模拟式模块")
private ApplicationModule simulate;
/**
* 数字式模块
*/
@ApiModelProperty("数字式模块")
private ApplicationModule digital;
/**
* 比对式模块
*/
@ApiModelProperty("比对式模块")
private ApplicationModule contrast;
}

View File

@@ -1,17 +0,0 @@
package com.njcn.gather.tool.active.vo;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@Data
public class ApplicationModule {
/**
* 是否申请 1是 0否
*/
@ApiModelProperty(value = "是否申请 1是 0否")
private int apply;
public boolean isApply() {
return apply == 1;
}
}