|
@@ -8,34 +8,39 @@ import com.genersoft.iot.vmp.VManageBootstrap;
|
|
|
import net.roseboy.classfinal.util.*;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.util.Base64Utils;
|
|
|
|
|
|
import java.io.*;
|
|
|
-
|
|
|
+import com.genersoft.iot.vmp.utils.DateUtil;
|
|
|
+import com.genersoft.iot.vmp.utils.Md5Utils;
|
|
|
import java.util.List;
|
|
|
import java.util.Scanner;
|
|
|
|
|
|
+
|
|
|
// 程序授权类. 用于控制程序的使用权限.
|
|
|
public class Authorize {
|
|
|
|
|
|
+
|
|
|
// 调用 lib/classfinal-fatjar.jar 中的方法. 用于获取机器码
|
|
|
- private final static Logger logger = LoggerFactory.getLogger(VManageBootstrap.class);
|
|
|
+ private final static Logger logger = LoggerFactory.getLogger(Authorize.class);
|
|
|
|
|
|
+ private static String upCompany = "szhfy";
|
|
|
|
|
|
public static String baseAuthorizeFilePath = "authorize/authorize.hfy";
|
|
|
|
|
|
+ public static String baseMachineFilePath = "authorize/MachineCode.hfy";
|
|
|
+
|
|
|
+ // 授权请求文件路径 (用于请求授权)
|
|
|
private String authorizeFilePath = "authorize/authorize.hfy";
|
|
|
|
|
|
- /**
|
|
|
- * 生成机器码 96 位
|
|
|
- */
|
|
|
- public static String getMachineCode() {
|
|
|
- String code = new String(SysUtils.makeMarchinCode());
|
|
|
- return code;
|
|
|
+
|
|
|
+ Authorize(String authorizeFilePath) {
|
|
|
+ this.authorizeFilePath = authorizeFilePath;
|
|
|
}
|
|
|
|
|
|
// 读取授权文件
|
|
|
- public static byte[] readAuthorizeFile(String path) {
|
|
|
+ private static byte[] readAuthorizeFile(String path) {
|
|
|
if(path == null) {
|
|
|
path = baseAuthorizeFilePath;
|
|
|
}
|
|
@@ -46,40 +51,77 @@ public class Authorize {
|
|
|
return null;
|
|
|
}
|
|
|
byte[] data = IoUtils.readFileToByte(file);
|
|
|
-// String code = new String(data);
|
|
|
-// logger.info("code: {}", code);
|
|
|
-// if(code.length() == 0) {
|
|
|
-// return null;
|
|
|
-// }
|
|
|
// 解析授权文件
|
|
|
return data;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 生成机器码 96 位
|
|
|
+ */
|
|
|
+ public static String getMachineCode() {
|
|
|
+ String code = new String(SysUtils.makeMarchinCode());
|
|
|
+ return code;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getAuthorizePasswd(String machineCode){
|
|
|
+ return machineCode.substring(16, 32) + machineCode + machineCode.substring(64, 80);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 检查授权文件是否存在
|
|
|
+ public static boolean checkAuthorizeFile(String path) {
|
|
|
+ if(path == null) {
|
|
|
+ path = baseAuthorizeFilePath;
|
|
|
+ }
|
|
|
+ File file = new File(path);
|
|
|
+ if(!file.exists()) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取授权文件密文
|
|
|
+ * @param authorizePath 授权文件路径
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getEncryptAuthorData(String authorizePath) {
|
|
|
+ byte[] authorizeCode = readAuthorizeFile(authorizePath);
|
|
|
+ if (authorizeCode == null || authorizeCode.length == 0) {
|
|
|
+ logger.error("授权文件读取失败. 请检查授权文件是否存在. ");
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ // 使用 base64 转换文本
|
|
|
+ byte[] data = Base64Utils.encode(authorizeCode);
|
|
|
+ String code = new String(data);
|
|
|
+ logger.info("code: {}", code);
|
|
|
+ return code;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 解密授权文件
|
|
|
* @param authorizePath 授权文件路径
|
|
|
* @return
|
|
|
*/
|
|
|
- public static String decryptAuthorizeFile(String authorizePath) {
|
|
|
+ public static String decryptAuthorizeFile(String machineCode, String authorizePath) {
|
|
|
+ byte[] authorizeCode = readAuthorizeFile(authorizePath);
|
|
|
+ if (machineCode == null) {
|
|
|
+ logger.error("机器码解析失败");
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ if(authorizeCode == null || authorizeCode.length == 0) {
|
|
|
+ logger.error("授权文件读取失败. 请检查授权文件是否存在. ");
|
|
|
+ return null;
|
|
|
+ }
|
|
|
try{
|
|
|
- String machineCode = getMachineCode();
|
|
|
- byte[] authorizeCode = readAuthorizeFile(authorizePath);
|
|
|
- if (machineCode == null) {
|
|
|
- logger.error("机器码获取失败. 请检查联系管理员. ");
|
|
|
- return null;
|
|
|
- }
|
|
|
- if(authorizeCode == null || authorizeCode.length == 0) {
|
|
|
- logger.error("授权文件读取失败. 请检查授权文件是否存在. ");
|
|
|
- return null;
|
|
|
- }
|
|
|
// 使用 base64 转换文本
|
|
|
byte[] data = Base64Utils.decode(authorizeCode);
|
|
|
String code = new String(data);
|
|
|
- logger.info("code: {}", code);
|
|
|
+// logger.info("code: {}", code);
|
|
|
// 解密授权文件
|
|
|
- String password = machineCode.substring(16, 32) + machineCode + machineCode.substring(64, 80);
|
|
|
+ String password = getAuthorizePasswd(machineCode);
|
|
|
String decryptStr = AesUtils.decrypt(code, password);
|
|
|
- logger.info("decryptStr: {}", decryptStr);
|
|
|
+// logger.info("decryptStr: {}", decryptStr);
|
|
|
return decryptStr;
|
|
|
}catch (Exception e) {
|
|
|
logger.error("授权文件解析失败. 请检查练习管理员进行排查. ");
|
|
@@ -89,34 +131,40 @@ public class Authorize {
|
|
|
}
|
|
|
|
|
|
// 生成授权文件
|
|
|
- public static boolean generateAuthorizeFile(String authorizePath, String authorizeCode) {
|
|
|
+ public static boolean saveAuthorizeFile(String authorizePath, String authorizeJson) {
|
|
|
try{
|
|
|
String machineCode = getMachineCode();
|
|
|
if (machineCode == null) {
|
|
|
logger.error("机器码获取失败. 请检查联系管理员. ");
|
|
|
return false;
|
|
|
}
|
|
|
- if(authorizeCode == null || authorizeCode.length() == 0) {
|
|
|
+ if(authorizeJson == null || authorizeJson.length() == 0) {
|
|
|
logger.error("授权码获取失败. 请检查授权码是否正确. ");
|
|
|
return false;
|
|
|
}
|
|
|
// 机器码长度转换 96 位 到128位 从16位 开始 取 16个字符 添加至机器码前方 再从 64位开始取 16个字符 添加至机器码后方
|
|
|
- String key = machineCode.substring(16, 32) + machineCode + machineCode.substring(64, 80);
|
|
|
- logger.info("key: {}", key);
|
|
|
- logger.info("key length {}", key.length());
|
|
|
+ String key = getAuthorizePasswd(machineCode);
|
|
|
// 解密授权文件
|
|
|
- String encryptStr = AesUtils.encrypt(authorizeCode, key);
|
|
|
- logger.info("encryptStr: {}", encryptStr);
|
|
|
+ String encryptStr = AesUtils.encrypt(authorizeJson, key);
|
|
|
// 使用 base64 转换文本
|
|
|
byte[] data = Base64Utils.encode(encryptStr.getBytes());
|
|
|
String code = new String(data);
|
|
|
- logger.info("code: {}", code);
|
|
|
- // 写入文件
|
|
|
+
|
|
|
+ // 授权数据写入文件
|
|
|
File file = new File(authorizePath);
|
|
|
- if(!file.exists()) {
|
|
|
- file.createNewFile();
|
|
|
+ if(file.exists()) {
|
|
|
+ file.delete();
|
|
|
}
|
|
|
+ file.createNewFile();
|
|
|
IoUtils.writeTxtFile(file, code);
|
|
|
+
|
|
|
+ // 机器码写入文件
|
|
|
+ File machineFile = new File(baseMachineFilePath);
|
|
|
+ if(machineFile.exists()) {
|
|
|
+ machineFile.delete();
|
|
|
+ }
|
|
|
+ machineFile.createNewFile();
|
|
|
+ IoUtils.writeTxtFile(machineFile, machineCode);
|
|
|
return true;
|
|
|
}catch (Exception e) {
|
|
|
logger.error("授权文件生成失败. 请检查练习管理员进行排查. ");
|
|
@@ -126,16 +174,67 @@ public class Authorize {
|
|
|
}
|
|
|
|
|
|
// 基础授权文件解析
|
|
|
- public static authorData parseBaseAuthorizeFile(String authorizePath) {
|
|
|
- String data = decryptAuthorizeFile(authorizePath);
|
|
|
+ public static AuthorData parseBaseAuthorizeFile(String machineCode, String authorizePath) {
|
|
|
+ String data = decryptAuthorizeFile(machineCode, authorizePath);
|
|
|
if(data == null) {
|
|
|
return null;
|
|
|
}
|
|
|
+// logger.info("data: {}", data);
|
|
|
// json 解析 fastjson2 依赖
|
|
|
- JSONObject = JSON.parse(data);
|
|
|
+ AuthorData authorData = JSON.parseObject(data, AuthorData.class);
|
|
|
+ // 输出解析结果
|
|
|
+ logger.info("机器码: {}", authorData.getMachineCode());
|
|
|
+ logger.info("公司名: {}", authorData.getCompany());
|
|
|
+ logger.info("授权码: {}", authorData.getAuthorizeCode());
|
|
|
+ logger.info("请求授权时间: {}", authorData.getAuthorizeTime());
|
|
|
+// logger.info("用户密码: {}", authorData.getPassword());
|
|
|
return authorData;
|
|
|
}
|
|
|
|
|
|
|
|
|
+ // 创建授权文件
|
|
|
+ public static boolean generateAuthorizeFile(String authorizePath, String company, String userPassword) {
|
|
|
+ String machineCode = getMachineCode();
|
|
|
+ AuthorData authorData = new AuthorData();
|
|
|
+ if (machineCode == null) {
|
|
|
+ logger.error("机器码获取失败. 请检查联系管理员. ");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ if(company == null || company.length() == 0) {
|
|
|
+ logger.error("公司名获取失败. 请检查公司名是否正确. ");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ if(userPassword == null || userPassword.length() == 0) {
|
|
|
+ logger.error("用户密码获取失败. 请检查用户密码是否正确. ");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ authorData.setMachineCode(machineCode);
|
|
|
+ authorData.setCompany(company);
|
|
|
+ authorData.setPassword(userPassword);
|
|
|
+ // 获取当前时间,使用 ISO8601 格式
|
|
|
+ authorData.setAuthorizeTime(DateUtil.getNowForISO8601());
|
|
|
+ // 构建授权码 md5(机器码 + 公司名 + 授权时间)
|
|
|
+ String authorizeCode = Md5Utils.hash(upCompany + machineCode + company + authorData.getAuthorizeTime());
|
|
|
+ authorData.setAuthorizeCode(authorizeCode);
|
|
|
+ // 转为 json 字符
|
|
|
+ String jsonStr = JSON.toJSONString(authorData);
|
|
|
+ return saveAuthorizeFile(authorizePath, jsonStr);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 读取授权批准文件
|
|
|
+ public static byte[] readAuthorizeApproveFile(String path) {
|
|
|
+ if(path == null) {
|
|
|
+ path = baseAuthorizeFilePath;
|
|
|
+ }
|
|
|
+ logger.info("path: {}", path);
|
|
|
+ File file = new File(path);
|
|
|
+ if(!file.exists()) {
|
|
|
+ logger.error("无法获取到授权批准文件. 请检查授权批准文件是否存在.");
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ byte[] data = IoUtils.readFileToByte(file);
|
|
|
+ // 解析授权文件
|
|
|
+ return data;
|
|
|
+ }
|
|
|
|
|
|
}
|