|
@@ -0,0 +1,141 @@
|
|
|
+package com.genersoft.iot.vmp.conf;
|
|
|
+
|
|
|
+//package net.roseboy.classfinal;
|
|
|
+
|
|
|
+
|
|
|
+import com.alibaba.fastjson2.JSON;
|
|
|
+import com.genersoft.iot.vmp.VManageBootstrap;
|
|
|
+import net.roseboy.classfinal.util.*;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.util.Base64Utils;
|
|
|
+
|
|
|
+import java.io.*;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+import java.util.Scanner;
|
|
|
+
|
|
|
+// 程序授权类. 用于控制程序的使用权限.
|
|
|
+public class Authorize {
|
|
|
+
|
|
|
+// 调用 lib/classfinal-fatjar.jar 中的方法. 用于获取机器码
|
|
|
+ private final static Logger logger = LoggerFactory.getLogger(VManageBootstrap.class);
|
|
|
+
|
|
|
+
|
|
|
+ public static String baseAuthorizeFilePath = "authorize/authorize.hfy";
|
|
|
+
|
|
|
+ private String authorizeFilePath = "authorize/authorize.hfy";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成机器码 96 位
|
|
|
+ */
|
|
|
+ public static String getMachineCode() {
|
|
|
+ String code = new String(SysUtils.makeMarchinCode());
|
|
|
+ return code;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 读取授权文件
|
|
|
+ public static byte[] readAuthorizeFile(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);
|
|
|
+// String code = new String(data);
|
|
|
+// logger.info("code: {}", code);
|
|
|
+// if(code.length() == 0) {
|
|
|
+// return null;
|
|
|
+// }
|
|
|
+ // 解析授权文件
|
|
|
+ return data;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 解密授权文件
|
|
|
+ * @param authorizePath 授权文件路径
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String decryptAuthorizeFile(String authorizePath) {
|
|
|
+ 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);
|
|
|
+ // 解密授权文件
|
|
|
+ String password = machineCode.substring(16, 32) + machineCode + machineCode.substring(64, 80);
|
|
|
+ String decryptStr = AesUtils.decrypt(code, password);
|
|
|
+ logger.info("decryptStr: {}", decryptStr);
|
|
|
+ return decryptStr;
|
|
|
+ }catch (Exception e) {
|
|
|
+ logger.error("授权文件解析失败. 请检查练习管理员进行排查. ");
|
|
|
+ logger.error("错误信息: {}", e.getMessage());
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 生成授权文件
|
|
|
+ public static boolean generateAuthorizeFile(String authorizePath, String authorizeCode) {
|
|
|
+ try{
|
|
|
+ String machineCode = getMachineCode();
|
|
|
+ if (machineCode == null) {
|
|
|
+ logger.error("机器码获取失败. 请检查联系管理员. ");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ if(authorizeCode == null || authorizeCode.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 encryptStr = AesUtils.encrypt(authorizeCode, key);
|
|
|
+ logger.info("encryptStr: {}", encryptStr);
|
|
|
+ // 使用 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();
|
|
|
+ }
|
|
|
+ IoUtils.writeTxtFile(file, code);
|
|
|
+ return true;
|
|
|
+ }catch (Exception e) {
|
|
|
+ logger.error("授权文件生成失败. 请检查练习管理员进行排查. ");
|
|
|
+ logger.error("错误信息: {}", e.getMessage());
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 基础授权文件解析
|
|
|
+ public static authorData parseBaseAuthorizeFile(String authorizePath) {
|
|
|
+ String data = decryptAuthorizeFile(authorizePath);
|
|
|
+ if(data == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ // json 解析 fastjson2 依赖
|
|
|
+ JSONObject = JSON.parse(data);
|
|
|
+ return authorData;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|