ADD: 程序激活流程:1、生成设备申请码;2、验证设备激活码;3、读取许可信息;4、激活码生成工具。

This commit is contained in:
贾同学
2025-10-14 18:54:32 +08:00
parent 3d4fb44b3a
commit 014ac7931e
22 changed files with 670 additions and 4 deletions

View File

@@ -0,0 +1,50 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.njcn.gather</groupId>
<artifactId>CN_Gather</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>activate-tool</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.njcn</groupId>
<artifactId>njcn-common</artifactId>
<version>0.0.1</version>
</dependency>
<dependency>
<groupId>com.njcn</groupId>
<artifactId>spingboot2.3.12</artifactId>
<version>2.3.12</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,15 @@
package com.njcn.gather.tool.active;
import com.njcn.gather.tool.active.config.RSAProperties;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
@EnableConfigurationProperties(RSAProperties.class)
@SpringBootApplication
public class ActivateToolApplication {
public static void main(String[] args) {
SpringApplication.run(ActivateToolApplication.class, args);
}
}

View File

@@ -0,0 +1,17 @@
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

@@ -0,0 +1,42 @@
package com.njcn.gather.tool.active.controller;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.gather.tool.active.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;
@ApiOperation("生成设备激活码")
@ApiImplicitParam(name = "params", value = "参数", required = true, dataType = "ApplicationCodeParams")
@PostMapping("/generateActivationCode")
public HttpResult generateActivationCode(@RequestBody ApplicationCodeParams params) {
String activationCode = activateService.generateActivationCode(params.getApplicationCode(), params.getPermanently());
return HttpResultUtil.assembleResult(CommonResponseEnum.SUCCESS.getCode(), activationCode, "");
}
@ApiModel("生成设备激活码参数")
@Data
public static class ApplicationCodeParams {
@ApiModelProperty(value = "设备申请码", required = true)
private String applicationCode;
@ApiModelProperty(value = "是否永久激活", example = "1", required = true)
private int permanently;
}
}

View File

@@ -0,0 +1,15 @@
package com.njcn.gather.tool.active.service;
public interface ActivateService {
/**
* 生成设备激活码
*
* @param applicationCode 申请码
* @param permanently 是否永久激活
* @return
*/
String generateActivationCode(String applicationCode, int permanently);
}

View File

@@ -0,0 +1,65 @@
package com.njcn.gather.tool.active.service.impl;
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.service.ActivateService;
import com.njcn.gather.tool.active.vo.ActivationCodePlaintext;
import com.njcn.gather.tool.active.vo.ActivationModule;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@RequiredArgsConstructor
@Slf4j
@Service
public class ActivateServiceImpl implements ActivateService {
private final RSAProperties rsaProperties;
@Override
public String generateActivationCode(String applicationCode, int permanently) {
// RSA 解密
String plaintext;
try {
plaintext = RSAUtil.decrypt(applicationCode, RSAUtil.stringToPrivateKey(rsaProperties.getPrivateKey()));
} catch (Exception e) {
log.error("申请码解密失败", e);
throw new BusinessException(CommonResponseEnum.FAIL, "无效的申请码");
}
ActivationCodePlaintext activationCodePlaintext = JSONUtil.toBean(plaintext, ActivationCodePlaintext.class);
if (activationCodePlaintext == null) {
log.error("申请码内容为空");
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);
}
// RSA 加密
String jsonStr = JSONUtil.toJsonStr(activationCodePlaintext);
log.info("生成激活码明文:{}", jsonStr);
try {
return RSAUtil.encrypt(jsonStr, RSAUtil.stringToPublicKey(rsaProperties.getPublicKey()));
} catch (Exception e) {
log.error("生成激活码失败", e);
throw new BusinessException(CommonResponseEnum.FAIL, "生成激活码失败");
}
}
}

View File

@@ -0,0 +1,28 @@
package com.njcn.gather.tool.active.vo;
import lombok.Data;
@Data
public class ActivationCodePlaintext {
/**
* mac地址
*/
private String macAddress;
/**
* 模拟式模块
*/
private ActivationModule simulate;
/**
* 数字式模块
*/
private ActivationModule digital;
/**
* 比对式模块
*/
private ActivationModule contrast;
}

View File

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

View File

@@ -0,0 +1,28 @@
package com.njcn.gather.tool.active.vo;
import lombok.Data;
@Data
public class ApplicationCodePlaintext {
/**
* mac地址
*/
private String macAddress;
/**
* 模拟式模块
*/
private ApplicationModule simulate;
/**
* 数字式模块
*/
private ApplicationModule digital;
/**
* 比对式模块
*/
private ApplicationModule contrast;
}

View File

@@ -0,0 +1,15 @@
package com.njcn.gather.tool.active.vo;
import lombok.Data;
@Data
public class ApplicationModule {
/**
* 是否申请 1是 0否
*/
private Integer apply;
public boolean isApply() {
return apply == 1;
}
}

View File

@@ -0,0 +1,8 @@
server:
port: 8888
spring:
application:
name: active-tool
rsa:
private-key: "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCcUyYhVqczGxblL+o/xZzF/8nf+LjrfUE/dS1aRHM7uMDD0cgCArhjtfneFePrMxt+Z7W8yNBzSarub8qsfhaVNikV7Es7oaeTygfjQXTi2n4AFkir3fM07J08RpWhl5M8f8uWTCuvFUYAw00gq55typqmnbkmJa2VIUy/iQf+cMCP7abz4/jNhUzUR3qA7TV4oMRgTdIEDUp63YF8dOC+JH8XxYrCVeHXV6fLCwmesdMzl0lB2VTEKMfLbXhOmF5g7P9y/16VCcN8UBuZlbyYfn+GAxJOSbeHi5HshOKfoSuD7Jz+3WQZpNavOWjIFExKIU38/CvnJCOP7XBCqpSTAgMBAAECggEAYeWokWRE3TpvwiOZnUpR/aVMdVi75a3ROL5XIpqPV61B+t/bU3cEpl0GF9C5pUeiRi0IoStZb3mI9D1KPW/REKyUWkhabQO1gFYbTnRlkNOn6M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