UPDATE: 程序激活码生成工具集成。
This commit is contained in:
@@ -1,17 +0,0 @@
|
||||
package com.njcn.gather.activate.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;
|
||||
}
|
||||
@@ -1,74 +0,0 @@
|
||||
package com.njcn.gather.activate.controller;
|
||||
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.njcn.common.pojo.annotation.OperateInfo;
|
||||
import com.njcn.common.pojo.enums.common.LogEnum;
|
||||
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
|
||||
import com.njcn.common.pojo.response.HttpResult;
|
||||
import com.njcn.common.utils.LogUtil;
|
||||
import com.njcn.gather.activate.pojo.vo.ActivationCodePlaintext;
|
||||
import com.njcn.gather.activate.pojo.vo.ApplicationCodePlaintext;
|
||||
import com.njcn.gather.activate.service.ActivateService;
|
||||
import com.njcn.web.controller.BaseController;
|
||||
import com.njcn.web.utils.HttpResultUtil;
|
||||
import io.swagger.annotations.*;
|
||||
import lombok.Data;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@Slf4j
|
||||
@Api(tags = "设备激活管理")
|
||||
@RestController
|
||||
@RequestMapping("/activate")
|
||||
@RequiredArgsConstructor
|
||||
public class ActivateController extends BaseController {
|
||||
|
||||
private final ActivateService activateService;
|
||||
|
||||
@OperateInfo(info = LogEnum.BUSINESS_COMMON)
|
||||
@ApiOperation("生成设备申请码")
|
||||
@ApiImplicitParam(name = "data", value = "生成设备申请码参数", required = true)
|
||||
@PostMapping("/generateApplicationCode")
|
||||
public HttpResult generateApplicationCode(@RequestBody ApplicationCodePlaintext data) {
|
||||
String methodDescribe = getMethodDescribe("generateApplicationCode");
|
||||
LogUtil.njcnDebug(log, "{},生成设备申请码:{}", methodDescribe, JSONUtil.toJsonStr(data));
|
||||
String applicationCode = activateService.generateApplicationCode(data);
|
||||
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, applicationCode, methodDescribe);
|
||||
}
|
||||
|
||||
@OperateInfo(info = LogEnum.BUSINESS_COMMON)
|
||||
@ApiOperation("验证设备激活码")
|
||||
@ApiImplicitParam(name = "params", value = "验证设备激活码参数", required = true)
|
||||
@PostMapping("/verifyActivationCode")
|
||||
public HttpResult verifyActivationCode(@RequestBody VerifyActivationCodeParams params) {
|
||||
String methodDescribe = getMethodDescribe("verifyActivationCode");
|
||||
LogUtil.njcnDebug(log, "{},验证设备激活码\":{}", methodDescribe, JSONUtil.toJsonStr(params));
|
||||
ActivationCodePlaintext activationCodePlaintext = activateService.verifyActivationCode(params.getActivationCode());
|
||||
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, activationCodePlaintext, methodDescribe);
|
||||
}
|
||||
|
||||
@OperateInfo(info = LogEnum.BUSINESS_COMMON)
|
||||
@ApiOperation("获取许可信息")
|
||||
@PostMapping("/getLicense")
|
||||
public HttpResult getLicense() {
|
||||
String methodDescribe = getMethodDescribe("checkLicense");
|
||||
LogUtil.njcnDebug(log, "{},获取许可信息", methodDescribe);
|
||||
ActivationCodePlaintext activationCodePlaintext = activateService.readLicenseFile();
|
||||
if (activationCodePlaintext == null) {
|
||||
activationCodePlaintext = new ActivationCodePlaintext();
|
||||
activationCodePlaintext.init();
|
||||
}
|
||||
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, activationCodePlaintext, methodDescribe);
|
||||
}
|
||||
|
||||
@ApiModel("验证设备激活码参数")
|
||||
@Data
|
||||
public static class VerifyActivationCodeParams {
|
||||
@ApiModelProperty(value = "激活码")
|
||||
private String activationCode;
|
||||
}
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
package com.njcn.gather.activate.pojo.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class ActivationCodePlaintext {
|
||||
/**
|
||||
* mac地址
|
||||
*/
|
||||
private String macAddress;
|
||||
|
||||
/**
|
||||
* 模拟式模块
|
||||
*/
|
||||
private ActivationModule simulate;
|
||||
|
||||
/**
|
||||
* 数字式模块
|
||||
*/
|
||||
private ActivationModule digital;
|
||||
|
||||
/**
|
||||
* 比对式模块
|
||||
*/
|
||||
private ActivationModule contrast;
|
||||
|
||||
|
||||
public void init() {
|
||||
simulate = new ActivationModule();
|
||||
digital = new ActivationModule();
|
||||
contrast = new ActivationModule();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
package com.njcn.gather.activate.pojo.vo;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Data
|
||||
public class ActivationModule extends ApplicationModule {
|
||||
|
||||
/**
|
||||
* 是否永久授权
|
||||
*/
|
||||
private int permanently;
|
||||
|
||||
public boolean isPermanently() {
|
||||
return permanently == 1;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
package com.njcn.gather.activate.pojo.vo;
|
||||
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
|
||||
@ApiModel("申请码明文")
|
||||
@Data
|
||||
public class ApplicationCodePlaintext {
|
||||
/**
|
||||
* mac地址
|
||||
*/
|
||||
@ApiModelProperty(value = "mac地址", hidden = true)
|
||||
private String macAddress;
|
||||
|
||||
/**
|
||||
* 模拟式模块
|
||||
*/
|
||||
@ApiModelProperty("模拟式模块")
|
||||
private ApplicationModule simulate;
|
||||
|
||||
/**
|
||||
* 数字式模块
|
||||
*/
|
||||
@ApiModelProperty("数字式模块")
|
||||
private ApplicationModule digital;
|
||||
|
||||
/**
|
||||
* 比对式模块
|
||||
*/
|
||||
@ApiModelProperty("比对式模块")
|
||||
private ApplicationModule contrast;
|
||||
|
||||
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
package com.njcn.gather.activate.pojo.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;
|
||||
}
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
package com.njcn.gather.activate.service;
|
||||
|
||||
import com.njcn.gather.activate.pojo.vo.ActivationCodePlaintext;
|
||||
import com.njcn.gather.activate.pojo.vo.ApplicationCodePlaintext;
|
||||
|
||||
public interface ActivateService {
|
||||
|
||||
/**
|
||||
* 生成设备申请码
|
||||
*
|
||||
* @param data
|
||||
* @return
|
||||
*/
|
||||
String generateApplicationCode(ApplicationCodePlaintext data);
|
||||
|
||||
/**
|
||||
* 验证激活码
|
||||
*
|
||||
* @param activationCode
|
||||
* @return
|
||||
*/
|
||||
ActivationCodePlaintext verifyActivationCode(String activationCode);
|
||||
|
||||
/**
|
||||
* 读取授权文件
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
ActivationCodePlaintext readLicenseFile();
|
||||
}
|
||||
@@ -1,134 +0,0 @@
|
||||
package com.njcn.gather.activate.service.impl;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.net.NetUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
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.activate.config.RSAProperties;
|
||||
import com.njcn.gather.activate.pojo.vo.ActivationCodePlaintext;
|
||||
import com.njcn.gather.activate.pojo.vo.ActivationModule;
|
||||
import com.njcn.gather.activate.pojo.vo.ApplicationCodePlaintext;
|
||||
import com.njcn.gather.activate.service.ActivateService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
@Service
|
||||
@EnableConfigurationProperties(RSAProperties.class)
|
||||
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;
|
||||
|
||||
@Override
|
||||
public String generateApplicationCode(ApplicationCodePlaintext data) {
|
||||
// 获取当前设备MAC地址
|
||||
String macAddress = NetUtil.getLocalMacAddress();
|
||||
log.debug("当前设备MAC地址:{}", macAddress);
|
||||
data.setMacAddress(macAddress);
|
||||
String plaintext = JSONUtil.toJsonStr(data);
|
||||
// RSA 加密
|
||||
try {
|
||||
return RSAUtil.encrypt(plaintext, RSAUtil.stringToPublicKey(rsaProperties.getPublicKey()));
|
||||
} catch (Exception e) {
|
||||
log.error("申请码加密失败", e);
|
||||
throw new BusinessException(CommonResponseEnum.FAIL, "申请码生成失败");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ActivationCodePlaintext verifyActivationCode(String activationCode) {
|
||||
String plaintext;
|
||||
try {
|
||||
plaintext = RSAUtil.decrypt(activationCode, RSAUtil.stringToPrivateKey(rsaProperties.getPrivateKey()));
|
||||
} catch (Exception e) {
|
||||
log.error("授权码解密失败", e);
|
||||
throw new BusinessException(CommonResponseEnum.FAIL, "无效的激活码");
|
||||
}
|
||||
log.info("新授权码解密:{}", JSONUtil.toJsonStr(plaintext));
|
||||
ActivationCodePlaintext activationCodePlaintext = JSONUtil.toBean(plaintext, ActivationCodePlaintext.class);
|
||||
String macAddress = NetUtil.getLocalMacAddress();
|
||||
if (!StrUtil.equals(activationCodePlaintext.getMacAddress(), macAddress)) {
|
||||
log.error("mac地址不匹配");
|
||||
throw new BusinessException(CommonResponseEnum.FAIL, "无效的激活码");
|
||||
}
|
||||
return addOrUpdateLicenseFile(activationCodePlaintext, activationCode);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ActivationCodePlaintext readLicenseFile() {
|
||||
if (FileUtil.exist(LICENSE_FILE_PATH)) {
|
||||
String content = FileUtil.readUtf8String(LICENSE_FILE_PATH);
|
||||
String plaintext;
|
||||
try {
|
||||
plaintext = RSAUtil.decrypt(content, RSAUtil.stringToPrivateKey(rsaProperties.getPrivateKey()));
|
||||
} catch (Exception e) {
|
||||
log.error("授权文件内容解密失败", e);
|
||||
throw new BusinessException(CommonResponseEnum.FAIL, "许可信息读取失败");
|
||||
}
|
||||
return JSONUtil.toBean(plaintext, ActivationCodePlaintext.class);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private ActivationCodePlaintext addOrUpdateLicenseFile(ActivationCodePlaintext newActivationCodePlaintext, String activationCode) {
|
||||
log.info("新授权码明文:{}", JSONUtil.toJsonStr(newActivationCodePlaintext));
|
||||
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, "无效的激活码");
|
||||
}
|
||||
log.info("授权文件路径:{}", LICENSE_FILE_PATH);
|
||||
ActivationCodePlaintext oldActivationCodePlaintext = this.readLicenseFile();
|
||||
if (oldActivationCodePlaintext == null) {
|
||||
// 如果文件不存在,创建新文件
|
||||
FileUtil.touch(LICENSE_FILE_PATH);
|
||||
// 写入授权文件
|
||||
FileUtil.writeUtf8String(activationCode, LICENSE_FILE_PATH);
|
||||
return newActivationCodePlaintext;
|
||||
} else {
|
||||
log.info("旧授权码明文:{}", JSONUtil.toJsonStr(oldActivationCodePlaintext));
|
||||
if (newContrast.isApply()) {
|
||||
oldActivationCodePlaintext.setContrast(newContrast);
|
||||
}
|
||||
if (newDigital.isApply()) {
|
||||
oldActivationCodePlaintext.setDigital(newDigital);
|
||||
}
|
||||
if (newSimulate.isApply()) {
|
||||
oldActivationCodePlaintext.setSimulate(newSimulate);
|
||||
}
|
||||
log.info("最新授权码明文:{}", JSONUtil.toJsonStr(oldActivationCodePlaintext));
|
||||
String updateContent;
|
||||
// 重新加密
|
||||
try {
|
||||
updateContent = RSAUtil.encrypt(JSONUtil.toJsonStr(oldActivationCodePlaintext), RSAUtil.stringToPublicKey(rsaProperties.getPublicKey()));
|
||||
} catch (Exception e) {
|
||||
log.error("授权文件内容加密失败", e);
|
||||
throw new BusinessException(CommonResponseEnum.FAIL, "激活失败,请联系管理员");
|
||||
}
|
||||
FileUtil.writeUtf8String(updateContent, LICENSE_FILE_PATH);
|
||||
return oldActivationCodePlaintext;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user