Authorize.java 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. package com.genersoft.iot.vmp.conf;
  2. //package net.roseboy.classfinal;
  3. import com.alibaba.fastjson2.JSON;
  4. import com.genersoft.iot.vmp.VManageBootstrap;
  5. import net.roseboy.classfinal.util.*;
  6. import org.slf4j.Logger;
  7. import org.slf4j.LoggerFactory;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.util.Base64Utils;
  10. import java.io.*;
  11. import com.genersoft.iot.vmp.utils.DateUtil;
  12. import com.genersoft.iot.vmp.utils.Md5Utils;
  13. import java.util.List;
  14. import java.util.Scanner;
  15. // 程序授权类. 用于控制程序的使用权限.
  16. public class Authorize {
  17. // 调用 lib/classfinal-fatjar.jar 中的方法. 用于获取机器码
  18. private final static Logger logger = LoggerFactory.getLogger(Authorize.class);
  19. private static String upCompany = "szhfy";
  20. public static String baseAuthorizeFilePath = "authorize/authorize.hfy";
  21. public static String baseMachineFilePath = "authorize/MachineCode.hfy";
  22. // 授权请求文件路径 (用于请求授权)
  23. private String authorizeFilePath = "authorize/authorize.hfy";
  24. Authorize(String authorizeFilePath) {
  25. this.authorizeFilePath = authorizeFilePath;
  26. }
  27. // 读取授权文件
  28. private static byte[] readAuthorizeFile(String path) {
  29. if(path == null) {
  30. path = baseAuthorizeFilePath;
  31. }
  32. logger.info("path: {}", path);
  33. File file = new File(path);
  34. if(!file.exists()) {
  35. logger.error("无法获取到授权文件. 请检查授权文件是否存在.");
  36. return null;
  37. }
  38. byte[] data = IoUtils.readFileToByte(file);
  39. // 解析授权文件
  40. return data;
  41. }
  42. /**
  43. * 生成机器码 96 位
  44. */
  45. public static String getMachineCode() {
  46. String code = new String(SysUtils.makeMarchinCode());
  47. return code;
  48. }
  49. public static String getAuthorizePasswd(String machineCode){
  50. return machineCode.substring(16, 32) + machineCode + machineCode.substring(64, 80);
  51. }
  52. // 检查授权文件是否存在
  53. public static boolean checkAuthorizeFile(String path) {
  54. if(path == null) {
  55. path = baseAuthorizeFilePath;
  56. }
  57. File file = new File(path);
  58. if(!file.exists()) {
  59. return false;
  60. }
  61. return true;
  62. }
  63. /**
  64. * 获取授权文件密文
  65. * @param authorizePath 授权文件路径
  66. * @return
  67. */
  68. public static String getEncryptAuthorData(String authorizePath) {
  69. byte[] authorizeCode = readAuthorizeFile(authorizePath);
  70. if (authorizeCode == null || authorizeCode.length == 0) {
  71. logger.error("授权文件读取失败. 请检查授权文件是否存在. ");
  72. return null;
  73. }
  74. // 使用 base64 转换文本
  75. byte[] data = Base64Utils.encode(authorizeCode);
  76. String code = new String(data);
  77. logger.info("code: {}", code);
  78. return code;
  79. }
  80. /**
  81. * 解密授权文件
  82. * @param authorizePath 授权文件路径
  83. * @return
  84. */
  85. public static String decryptAuthorizeFile(String machineCode, String authorizePath) {
  86. byte[] authorizeCode = readAuthorizeFile(authorizePath);
  87. if (machineCode == null) {
  88. logger.error("机器码解析失败");
  89. return null;
  90. }
  91. if(authorizeCode == null || authorizeCode.length == 0) {
  92. logger.error("授权文件读取失败. 请检查授权文件是否存在. ");
  93. return null;
  94. }
  95. try{
  96. // 使用 base64 转换文本
  97. byte[] data = Base64Utils.decode(authorizeCode);
  98. String code = new String(data);
  99. // logger.info("code: {}", code);
  100. // 解密授权文件
  101. String password = getAuthorizePasswd(machineCode);
  102. String decryptStr = AesUtils.decrypt(code, password);
  103. // logger.info("decryptStr: {}", decryptStr);
  104. return decryptStr;
  105. }catch (Exception e) {
  106. logger.error("授权文件解析失败. 请检查练习管理员进行排查. ");
  107. logger.error("错误信息: {}", e.getMessage());
  108. return null;
  109. }
  110. }
  111. // 生成授权文件
  112. public static boolean saveAuthorizeFile(String authorizePath, String authorizeJson) {
  113. try{
  114. String machineCode = getMachineCode();
  115. if (machineCode == null) {
  116. logger.error("机器码获取失败. 请检查联系管理员. ");
  117. return false;
  118. }
  119. if(authorizeJson == null || authorizeJson.length() == 0) {
  120. logger.error("授权码获取失败. 请检查授权码是否正确. ");
  121. return false;
  122. }
  123. // 机器码长度转换 96 位 到128位 从16位 开始 取 16个字符 添加至机器码前方 再从 64位开始取 16个字符 添加至机器码后方
  124. String key = getAuthorizePasswd(machineCode);
  125. // 解密授权文件
  126. String encryptStr = AesUtils.encrypt(authorizeJson, key);
  127. // 使用 base64 转换文本
  128. byte[] data = Base64Utils.encode(encryptStr.getBytes());
  129. String code = new String(data);
  130. // 授权数据写入文件
  131. File file = new File(authorizePath);
  132. if(file.exists()) {
  133. file.delete();
  134. }
  135. file.createNewFile();
  136. IoUtils.writeTxtFile(file, code);
  137. // 机器码写入文件
  138. File machineFile = new File(baseMachineFilePath);
  139. if(machineFile.exists()) {
  140. machineFile.delete();
  141. }
  142. machineFile.createNewFile();
  143. IoUtils.writeTxtFile(machineFile, machineCode);
  144. return true;
  145. }catch (Exception e) {
  146. logger.error("授权文件生成失败. 请检查练习管理员进行排查. ");
  147. logger.error("错误信息: {}", e.getMessage());
  148. return false;
  149. }
  150. }
  151. // 基础授权文件解析
  152. public static AuthorData parseBaseAuthorizeFile(String machineCode, String authorizePath) {
  153. String data = decryptAuthorizeFile(machineCode, authorizePath);
  154. if(data == null) {
  155. return null;
  156. }
  157. // logger.info("data: {}", data);
  158. // json 解析 fastjson2 依赖
  159. AuthorData authorData = JSON.parseObject(data, AuthorData.class);
  160. // 输出解析结果
  161. logger.info("机器码: {}", authorData.getMachineCode());
  162. logger.info("公司名: {}", authorData.getCompany());
  163. logger.info("授权码: {}", authorData.getAuthorizeCode());
  164. logger.info("请求授权时间: {}", authorData.getAuthorizeTime());
  165. // logger.info("用户密码: {}", authorData.getPassword());
  166. return authorData;
  167. }
  168. // 创建授权文件
  169. public static boolean generateAuthorizeFile(String authorizePath, String company, String userPassword) {
  170. String machineCode = getMachineCode();
  171. AuthorData authorData = new AuthorData();
  172. if (machineCode == null) {
  173. logger.error("机器码获取失败. 请检查联系管理员. ");
  174. return false;
  175. }
  176. if(company == null || company.length() == 0) {
  177. logger.error("公司名获取失败. 请检查公司名是否正确. ");
  178. return false;
  179. }
  180. if(userPassword == null || userPassword.length() == 0) {
  181. logger.error("用户密码获取失败. 请检查用户密码是否正确. ");
  182. return false;
  183. }
  184. authorData.setMachineCode(machineCode);
  185. authorData.setCompany(company);
  186. authorData.setPassword(userPassword);
  187. // 获取当前时间,使用 ISO8601 格式
  188. authorData.setAuthorizeTime(DateUtil.getNowForISO8601());
  189. // 构建授权码 md5(机器码 + 公司名 + 授权时间)
  190. String authorizeCode = Md5Utils.hash(upCompany + machineCode + company + authorData.getAuthorizeTime());
  191. authorData.setAuthorizeCode(authorizeCode);
  192. // 转为 json 字符
  193. String jsonStr = JSON.toJSONString(authorData);
  194. return saveAuthorizeFile(authorizePath, jsonStr);
  195. }
  196. // 读取授权批准文件
  197. public static byte[] readAuthorizeApproveFile(String path) {
  198. if(path == null) {
  199. path = baseAuthorizeFilePath;
  200. }
  201. logger.info("path: {}", path);
  202. File file = new File(path);
  203. if(!file.exists()) {
  204. logger.error("无法获取到授权批准文件. 请检查授权批准文件是否存在.");
  205. return null;
  206. }
  207. byte[] data = IoUtils.readFileToByte(file);
  208. // 解析授权文件
  209. return data;
  210. }
  211. }