feat: 支持激活码生成
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
package org.wfc.common.license.controller;
|
package org.wfc.common.license.controller;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
@@ -15,6 +16,10 @@ import org.wfc.common.license.license.LicenseCreator;
|
|||||||
import org.wfc.common.license.license.LinuxServerInfos;
|
import org.wfc.common.license.license.LinuxServerInfos;
|
||||||
import org.wfc.common.license.license.WindowsServerInfos;
|
import org.wfc.common.license.license.WindowsServerInfos;
|
||||||
|
|
||||||
|
import javax.crypto.Cipher;
|
||||||
|
import javax.crypto.spec.SecretKeySpec;
|
||||||
|
import java.security.MessageDigest;
|
||||||
|
import java.util.Base64;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@@ -28,6 +33,8 @@ import java.util.Map;
|
|||||||
@RequestMapping("/license")
|
@RequestMapping("/license")
|
||||||
public class LicenseCreatorController {
|
public class LicenseCreatorController {
|
||||||
|
|
||||||
|
private static final String key = "wfcwanfiAdmin6666";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 证书生成路径
|
* 证书生成路径
|
||||||
*/
|
*/
|
||||||
@@ -98,9 +105,18 @@ public class LicenseCreatorController {
|
|||||||
* @return java.util.Map<java.lang.String, java.lang.Object>
|
* @return java.util.Map<java.lang.String, java.lang.Object>
|
||||||
*/
|
*/
|
||||||
@RequestMapping(value = "/generateLicense", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
|
@RequestMapping(value = "/generateLicense", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
|
||||||
public Map<String, Object> generateLicense(@RequestBody LicenseGenParam genParam) {
|
public Map<String, Object> generateLicense(@RequestBody LicenseGenParam genParam) throws Exception {
|
||||||
Map<String, Object> resultMap = new HashMap<>(2);
|
Map<String, Object> resultMap = new HashMap<>(2);
|
||||||
|
|
||||||
|
LicenseCheckModel licenseParam = new LicenseCheckModel();
|
||||||
|
if (StringUtils.isNotBlank(genParam.getActivationCode())) {
|
||||||
|
// 解密
|
||||||
|
String decryptedJson = decrypt(genParam.getActivationCode());
|
||||||
|
// 反序列化为对象
|
||||||
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
|
licenseParam = mapper.readValue(decryptedJson, LicenseCheckModel.class);
|
||||||
|
}
|
||||||
|
|
||||||
LicenseCreatorParam param = new LicenseCreatorParam();
|
LicenseCreatorParam param = new LicenseCreatorParam();
|
||||||
param.setSubject("license_wfc");
|
param.setSubject("license_wfc");
|
||||||
param.setPrivateAlias("privateKey");
|
param.setPrivateAlias("privateKey");
|
||||||
@@ -122,10 +138,10 @@ public class LicenseCreatorController {
|
|||||||
param.setConsumerAmount(1);
|
param.setConsumerAmount(1);
|
||||||
param.setDescription("这是证书描述信息");
|
param.setDescription("这是证书描述信息");
|
||||||
LicenseCheckModel licenseCheckModel = new LicenseCheckModel();
|
LicenseCheckModel licenseCheckModel = new LicenseCheckModel();
|
||||||
licenseCheckModel.setIpAddress(genParam.getIpAddress());
|
// licenseCheckModel.setIpAddress(genParam.getIpAddress());
|
||||||
licenseCheckModel.setMacAddress(genParam.getMacAddress());
|
// licenseCheckModel.setMacAddress(genParam.getMacAddress());
|
||||||
licenseCheckModel.setCpuSerial(genParam.getCpuSerial());
|
licenseCheckModel.setCpuSerial(licenseParam.getCpuSerial());
|
||||||
licenseCheckModel.setMainBoardSerial(genParam.getMainBoardSerial());
|
licenseCheckModel.setMainBoardSerial(licenseParam.getMainBoardSerial());
|
||||||
param.setLicenseCheckModel(licenseCheckModel);
|
param.setLicenseCheckModel(licenseCheckModel);
|
||||||
|
|
||||||
LicenseCreator licenseCreator = new LicenseCreator(param);
|
LicenseCreator licenseCreator = new LicenseCreator(param);
|
||||||
@@ -140,4 +156,18 @@ public class LicenseCreatorController {
|
|||||||
}
|
}
|
||||||
return resultMap;
|
return resultMap;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String decrypt(String encryptedJson) throws Exception {
|
||||||
|
byte[] keyBytes = key.getBytes();
|
||||||
|
MessageDigest sha = MessageDigest.getInstance("SHA-256");
|
||||||
|
keyBytes = sha.digest(keyBytes);
|
||||||
|
keyBytes = java.util.Arrays.copyOf(keyBytes, 16);
|
||||||
|
SecretKeySpec secretKey = new SecretKeySpec(keyBytes, "AES");
|
||||||
|
|
||||||
|
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
|
||||||
|
cipher.init(Cipher.DECRYPT_MODE, secretKey);
|
||||||
|
byte[] encryptedBytes = Base64.getDecoder().decode(encryptedJson);
|
||||||
|
byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
|
||||||
|
return new String(decryptedBytes);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -70,4 +70,29 @@ public class LicenseCheckModel implements Serializable {
|
|||||||
", mainBoardSerial='" + mainBoardSerial + '\'' +
|
", mainBoardSerial='" + mainBoardSerial + '\'' +
|
||||||
'}';
|
'}';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String toJsonString() {
|
||||||
|
StringBuilder jsonBuilder = new StringBuilder();
|
||||||
|
jsonBuilder.append("{");
|
||||||
|
// jsonBuilder.append("\"ipAddress\": [");
|
||||||
|
// for (String ip : ipAddress) {
|
||||||
|
// jsonBuilder.append("\"").append(ip).append("\", ");
|
||||||
|
// }
|
||||||
|
// jsonBuilder.deleteCharAt(jsonBuilder.length() - 1);
|
||||||
|
// jsonBuilder.deleteCharAt(jsonBuilder.length() - 1);
|
||||||
|
// jsonBuilder.append("], ");
|
||||||
|
// jsonBuilder.append("\"macAddress\": [");
|
||||||
|
// for (String mac : macAddress) {
|
||||||
|
// jsonBuilder.append("\"").append(mac).append("\", ");
|
||||||
|
// }
|
||||||
|
// jsonBuilder.deleteCharAt(jsonBuilder.length() - 1);
|
||||||
|
// jsonBuilder.deleteCharAt(jsonBuilder.length() - 1);
|
||||||
|
// jsonBuilder.append("], ");
|
||||||
|
jsonBuilder.append("\"cpuSerial\": \"").append(cpuSerial).append("\", ");
|
||||||
|
jsonBuilder.append("\"mainBoardSerial\": \"").append(mainBoardSerial).append("\"");
|
||||||
|
// jsonBuilder.append("\"registerAmount\": \"").append(registerAmount).append("\"");
|
||||||
|
jsonBuilder.append("}");
|
||||||
|
|
||||||
|
return jsonBuilder.toString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,11 @@ public class LicenseGenParam implements Serializable {
|
|||||||
*/
|
*/
|
||||||
private String licensePath;
|
private String licensePath;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 激活码
|
||||||
|
*/
|
||||||
|
private String activationCode;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 证书失效时间
|
* 证书失效时间
|
||||||
*/
|
*/
|
||||||
@@ -49,6 +54,14 @@ public class LicenseGenParam implements Serializable {
|
|||||||
*/
|
*/
|
||||||
private String mainBoardSerial;
|
private String mainBoardSerial;
|
||||||
|
|
||||||
|
public String getActivationCode() {
|
||||||
|
return activationCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setActivationCode(String activationCode) {
|
||||||
|
this.activationCode = activationCode;
|
||||||
|
}
|
||||||
|
|
||||||
public String getLicensePath() {
|
public String getLicensePath() {
|
||||||
return licensePath;
|
return licensePath;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,8 +26,8 @@ public abstract class AbstractServerInfos {
|
|||||||
LicenseCheckModel result = new LicenseCheckModel();
|
LicenseCheckModel result = new LicenseCheckModel();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
result.setIpAddress(this.getIpAddress());
|
// result.setIpAddress(this.getIpAddress());
|
||||||
result.setMacAddress(this.getMacAddress());
|
// result.setMacAddress(this.getMacAddress());
|
||||||
result.setCpuSerial(this.getCPUSerial());
|
result.setCpuSerial(this.getCPUSerial());
|
||||||
result.setMainBoardSerial(this.getMainBoardSerial());
|
result.setMainBoardSerial(this.getMainBoardSerial());
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|||||||
80
src/main/java/org/wfc/common/license/license/SeverInfo.java
Normal file
80
src/main/java/org/wfc/common/license/license/SeverInfo.java
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
package org.wfc.common.license.license;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import org.wfc.common.license.domain.LicenseCheckModel;
|
||||||
|
|
||||||
|
import javax.crypto.Cipher;
|
||||||
|
import javax.crypto.spec.SecretKeySpec;
|
||||||
|
import java.security.MessageDigest;
|
||||||
|
import java.util.Base64;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description:
|
||||||
|
* @author: cyc
|
||||||
|
* @since: 2025-04-17
|
||||||
|
*/
|
||||||
|
public class SeverInfo {
|
||||||
|
|
||||||
|
private static final String key = "wfcwanfiAdmin6666";
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
LicenseCheckModel serverInfos = AbstractServerInfos.getServer("").getServerInfos();
|
||||||
|
|
||||||
|
serverInfos.setCpuSerial("aaaa");
|
||||||
|
//对象序列化为json
|
||||||
|
String jsonString = serverInfos.toJsonString();
|
||||||
|
System.out.println("原始数据 = "+jsonString);
|
||||||
|
//加密
|
||||||
|
String encryptedJson = encrypt(jsonString);
|
||||||
|
System.out.println("加密后 = " + encryptedJson);
|
||||||
|
// 解密
|
||||||
|
String decryptedJson = decrypt(encryptedJson);
|
||||||
|
System.out.println("解密后 = " + decryptedJson);
|
||||||
|
// 比较解密后的字符串与原始字符串
|
||||||
|
if (jsonString.equals(decryptedJson)) {
|
||||||
|
System.out.println("解密后的字符串与原始字符串相同");
|
||||||
|
} else {
|
||||||
|
System.out.println("解密后的字符串与原始字符串不同");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 断言
|
||||||
|
assert jsonString.equals(decryptedJson) : "解密后的字符串与原始字符串不匹配";
|
||||||
|
|
||||||
|
// 反序列化为对象
|
||||||
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
|
LicenseCheckModel licenseParam = null;
|
||||||
|
licenseParam = mapper.readValue(decryptedJson, LicenseCheckModel.class);
|
||||||
|
// 标记为null的字段需要特殊处理
|
||||||
|
// if ("null".equals(licenseParam.getRegisterAmount())) {
|
||||||
|
// licenseParam.setRegisterAmount(null);
|
||||||
|
// }
|
||||||
|
System.out.println("licenseParam = " + licenseParam);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String encrypt(String json) throws Exception {
|
||||||
|
byte[] keyBytes = key.getBytes();
|
||||||
|
MessageDigest sha = MessageDigest.getInstance("SHA-256");
|
||||||
|
keyBytes = sha.digest(keyBytes);
|
||||||
|
keyBytes = java.util.Arrays.copyOf(keyBytes, 16);
|
||||||
|
SecretKeySpec secretKey = new SecretKeySpec(keyBytes, "AES");
|
||||||
|
|
||||||
|
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
|
||||||
|
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
|
||||||
|
byte[] encryptedBytes = cipher.doFinal(json.getBytes());
|
||||||
|
return Base64.getEncoder().encodeToString(encryptedBytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String decrypt(String encryptedJson) throws Exception {
|
||||||
|
byte[] keyBytes = key.getBytes();
|
||||||
|
MessageDigest sha = MessageDigest.getInstance("SHA-256");
|
||||||
|
keyBytes = sha.digest(keyBytes);
|
||||||
|
keyBytes = java.util.Arrays.copyOf(keyBytes, 16);
|
||||||
|
SecretKeySpec secretKey = new SecretKeySpec(keyBytes, "AES");
|
||||||
|
|
||||||
|
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
|
||||||
|
cipher.init(Cipher.DECRYPT_MODE, secretKey);
|
||||||
|
byte[] encryptedBytes = Base64.getDecoder().decode(encryptedJson);
|
||||||
|
byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
|
||||||
|
return new String(decryptedBytes);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user