Browse Source

MAC地址,更换端口

lchao 1 year ago
parent
commit
78331ddfc2

+ 1 - 1
authority-admin/src/main/java/com/ruoyi/web/controller/authority/ProjectAuthorityController.java

@@ -37,7 +37,7 @@ public class ProjectAuthorityController {
         SM4Utils sm4 = new SM4Utils();
         Map map = new HashMap<>();
         SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
-        String secretKey = sm4.decryptData_CBC(json.get("secretKey").toString());
+        String secretKey = sm4.decryptData_CBC(json.get("secretKey").toString(),json.get("macAddress").toString());
         SysSecretProject sysSecretProject = sysSecretProjectMapper.selectBaseBySecretKey(secretKey);
         Date now = new Date();
         Date date = dateFormat.parse(sysSecretProject.getOverdueDate().toString());

+ 28 - 28
authority-admin/src/main/java/com/ruoyi/web/controller/sm4/SM4Utils.java

@@ -158,7 +158,7 @@ public class SM4Utils {
      * @param cipherText
      * @return
      */
-    public String decryptData_CBC(String cipherText) {
+    public String decryptData_CBC(String cipherText,String mac) {
         try {
             byte[] encrypted = Util.hexToByte(cipherText);
             cipherText = Base64.encode(encrypted);
@@ -175,10 +175,10 @@ public class SM4Utils {
             byte[] keyBytes;
             byte[] ivBytes;
             if (hexString) {
-                keyBytes = Util.hexStringToBytes(secretKey);
+                keyBytes = Util.hexStringToBytes(mac);
                 ivBytes = Util.hexStringToBytes(iv);
             } else {
-                keyBytes = secretKey.getBytes();
+                keyBytes = mac.getBytes();
                 ivBytes = iv.getBytes();
             }
 
@@ -195,31 +195,31 @@ public class SM4Utils {
         }
     }
 
-    public static void main(String[] args) throws IOException {
-        String plainText = "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiLpobnnm64iLCJpYXQiOjE3MDA3MDQzMTQsImV4cCI6MTcwMTI3MzYwMH0.eWbknSZZnlpOiHhKtddjzMKl0FbUVFznV3OB6aanUlA";
-//        String s = Util.byteToHex(plainText.getBytes());
-//        System.out.println("原文" + s);
-        SM4Utils sm4 = new SM4Utils();
-        sm4.secretKey = "C97E76AF40C82627E4BDF23B2DB9D476";
-        sm4.hexString = true;
+//    public static void main(String[] args) throws IOException {
+//        String plainText = "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiLpobnnm64iLCJpYXQiOjE3MDA3MDQzMTQsImV4cCI6MTcwMTI3MzYwMH0.eWbknSZZnlpOiHhKtddjzMKl0FbUVFznV3OB6aanUlA";
+////        String s = Util.byteToHex(plainText.getBytes());
+////        System.out.println("原文" + s);
+//        SM4Utils sm4 = new SM4Utils();
+//        sm4.secretKey = "C97E76AF40C82627E4BDF23B2DB9D476";
+//        sm4.hexString = true;
+////
+////        System.out.println("ECB模式加密");
+////        String cipherText = sm4.encryptData_ECB(plainText);
+////        System.out.println("密文: " + cipherText);
+////        System.out.println();
+////
+////        System.out.println("ECB模式解密");
+////        String plainText1 = sm4.decryptData_ECB(cipherText);
+////        System.out.println("明文: " + plainText1);
+////        System.out.println();
 //
-//        System.out.println("ECB模式加密");
-//        String cipherText = sm4.encryptData_ECB(plainText);
-//        System.out.println("密文: " + cipherText);
-//        System.out.println();
+//        System.out.println("CBC模式加密");
+//        sm4.iv = "31313131313131313131313131313131";
+//        String cipherText2 = sm4.encryptData_CBC(plainText);
+//        System.out.println("加密密文: " + cipherText2);
 //
-//        System.out.println("ECB模式解密");
-//        String plainText1 = sm4.decryptData_ECB(cipherText);
-//        System.out.println("明文: " + plainText1);
-//        System.out.println();
-
-        System.out.println("CBC模式加密");
-        sm4.iv = "31313131313131313131313131313131";
-        String cipherText2 = sm4.encryptData_CBC(plainText);
-        System.out.println("加密密文: " + cipherText2);
-
-        String plainText3 = sm4.decryptData_CBC(cipherText2);
-        System.out.println("解密明文: " + plainText3);
-
-    }
+//        String plainText3 = sm4.decryptData_CBC(cipherText2);
+//        System.out.println("解密明文: " + plainText3);
+//
+//    }
 }

+ 5 - 2
authority-admin/src/main/java/com/ruoyi/web/controller/system/SysSecretProjectController.java

@@ -17,9 +17,12 @@ import io.swagger.annotations.ApiOperation;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 
+import java.net.NetworkInterface;
+import java.net.SocketException;
 import java.time.Duration;
 import java.time.LocalDate;
 import java.time.LocalDateTime;
+import java.util.Enumeration;
 import java.util.List;
 
 /**
@@ -87,7 +90,7 @@ public class SysSecretProjectController extends BaseController {
         json.set("projectId", sysSecretProjectDTO.getProjectId());
         json.set("projectName", sysSecretProjectDTO.getProjectName());
         json.set("overdueDate", sysSecretProjectDTO.getOverdueDate());
-        String jwt = JWTExample.createJWT(json.toString(), milliseconds);
+        String jwt = JWTExample.createJWT(json.toString(), milliseconds,sysSecretProjectDTO.getMacAddress());
         sysSecretProjectDTO.setSecretKey(jwt);
         int comparisonResult = sysSecretProjectDTO.getOverdueDate().compareTo(currentDate);
         if (comparisonResult > 0) {
@@ -114,7 +117,7 @@ public class SysSecretProjectController extends BaseController {
             json.set("projectId", sysSecretProjectDTO.getProjectId());
             json.set("projectName", sysSecretProjectDTO.getProjectName());
             json.set("overdueDate", sysSecretProjectDTO.getOverdueDate());
-            String jwt = JWTExample.createJWT(json.toString(), milliseconds);
+            String jwt = JWTExample.createJWT(json.toString(), milliseconds,sysSecretProjectDTO.getMacAddress());
             sysSecretProjectDTO.setSecretKey(jwt);
         }
 

+ 17 - 17
authority-admin/src/main/java/com/ruoyi/web/utils/JWTExample.java

@@ -8,23 +8,23 @@ import io.jsonwebtoken.SignatureAlgorithm;
 import java.util.Date;
 
 public class JWTExample {
-    private static final String SECRET_KEY = "C97E76AF40CSH12N128SN23B2DB9D476";
+//    private static final String SECRET_KEY = "C97E76AF40CSH12N128SN23B2DB9D476";
     private static final long EXPIRATION_TIME = 86400000; // 24小时
 
-    public static void main(String[] args) {
-        // 创建JWT
-        String token = createJWT("user123",EXPIRATION_TIME);
-
-        // 验证和解析JWT
-        if (validateJWT(token)) {
-            String username = parseJWT(token);
-            System.out.println("解析到的用户名:" + username);
-        } else {
-            System.out.println("JWT验证失败");
-        }
-    }
-
-    public static String createJWT(String str,Long dateTime) {
+//    public static void main(String[] args) {
+//        // 创建JWT
+//        String token = createJWT("user123",EXPIRATION_TIME);
+//
+//        // 验证和解析JWT
+//        if (validateJWT(token)) {
+//            String username = parseJWT(token);
+//            System.out.println("解析到的用户名:" + username);
+//        } else {
+//            System.out.println("JWT验证失败");
+//        }
+//    }
+
+    public static String createJWT(String str,Long dateTime,String SECRET_KEY) {
         Date now = new Date();
         Date expiration = new Date(now.getTime() + dateTime);
 
@@ -38,7 +38,7 @@ public class JWTExample {
         return token;
     }
 
-    public static boolean validateJWT(String token) {
+    public static boolean validateJWT(String token,String SECRET_KEY) {
         try {
             Jwts.parser().setSigningKey(SECRET_KEY).parseClaimsJws(token);
             return true;
@@ -47,7 +47,7 @@ public class JWTExample {
         }
     }
 
-    public static String parseJWT(String token) {
+    public static String parseJWT(String token,String SECRET_KEY) {
         Jws<Claims> claims = Jwts.parser().setSigningKey(SECRET_KEY).parseClaimsJws(token);
         return claims.getBody().getSubject();
     }

+ 1 - 1
authority-admin/src/main/resources/application.yml

@@ -18,7 +18,7 @@ ruoyi:
 # 开发环境配置
 server:
   # 服务器的HTTP端口,默认为8080
-  port: 8080
+  port: 8002
   servlet:
     # 应用的访问路径
     context-path: /

+ 10 - 0
authority-system/src/main/java/com/ruoyi/system/domain/SysSecretProject.java

@@ -56,6 +56,7 @@ public class SysSecretProject extends BaseEntity {
      * 授权认证类型(1:离线认证;2:在线认证;)
      */
     private Long authorityType;
+    private String macAddress;
 
     public void setId(Long id) {
         this.id = id;
@@ -116,6 +117,7 @@ public class SysSecretProject extends BaseEntity {
                 .append("secretKey", getSecretKey())
                 .append("isSwitch", getIsSwitch())
                 .append("authorityType", getAuthorityType())
+                .append("macAddress", getMacAddress())
                 .toString();
     }
 
@@ -126,4 +128,12 @@ public class SysSecretProject extends BaseEntity {
     public void setAuthorityType(Long authorityType) {
         this.authorityType = authorityType;
     }
+
+    public String getMacAddress() {
+        return macAddress;
+    }
+
+    public void setMacAddress(String macAddress) {
+        this.macAddress = macAddress;
+    }
 }

+ 15 - 0
authority-system/src/main/java/com/ruoyi/system/domain/dto/SysSecretProjectDTO.java

@@ -55,6 +55,11 @@ public class SysSecretProjectDTO extends BaseEntity {
     private String secretKey;
 
     /**
+     * mac地址
+     */
+    private String macAddress;
+
+    /**
      * 授权认证类型(1:离线认证;2:在线认证;)
      */
     private String authorityType;
@@ -138,6 +143,14 @@ public class SysSecretProjectDTO extends BaseEntity {
     public void setAuthorityType(String authorityType) {
         this.authorityType = authorityType;
     }
+
+    public String getMacAddress() {
+        return macAddress;
+    }
+
+    public void setMacAddress(String macAddress) {
+        this.macAddress = macAddress;
+    }
     @Override
     public String toString() {
         return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
@@ -151,8 +164,10 @@ public class SysSecretProjectDTO extends BaseEntity {
                 .append("isSwitch", getIsSwitch())
                 .append("value", getValue())
                 .append("authorityType", getAuthorityType())
+                .append("macAddress", getMacAddress())
                 .toString();
     }
 
 
+
 }

+ 10 - 3
authority-system/src/main/resources/mapper/system/SysSecretProjectMapper.xml

@@ -12,6 +12,7 @@
         <result property="secretKey" column="secret_key"/>
         <result property="isSwitch" column="is_switch"/>
         <result property="authorityType" column="authority_type"/>
+        <result property="macAddress" column="mac_address"/>
     </resultMap>
 
     <resultMap type="SysSecretProjectDTO" id="getSysSecretProjectList">
@@ -24,10 +25,11 @@
         <result property="statusText" column="statusText"/>
         <result property="isSwitch" column="is_switch"/>
         <result property="authorityType" column="authority_type"/>
+        <result property="macAddress" column="mac_address"/>
     </resultMap>
 
     <sql id="selectSysSecretProjectVo">
-        select id, project_id, overdue_date, status, secret_key,is_switch,authority_type
+        select id, project_id, overdue_date, status, secret_key,is_switch,authority_type,mac_address
         from sys_secret_project
     </sql>
 
@@ -43,7 +45,8 @@
         ssp.status,
         ssp.is_switch,
         secret_key,
-        ssp.authority_type
+        ssp.authority_type,
+        ssp.mac_address
         from sys_secret_project ssp
         left join sys_project sp on ssp.project_id = sp.id
         <where>
@@ -65,7 +68,8 @@
                ssp.status,
                ssp.is_switch,
                secret_key,
-               ssp.authority_type
+               ssp.authority_type,
+               ssp.mac_address
         from sys_secret_project ssp
                  left join sys_project sp on ssp.project_id = sp.id
         where ssp.id = #{id}
@@ -80,6 +84,7 @@
             <if test="overdueDate != null">overdue_date,</if>
             <if test="isSwitch != null">is_switch,</if>
             <if test="authorityType != null">authority_type,</if>
+            <if test="macAddress != null">mac_address,</if>
         </trim>
         <trim prefix="values (" suffix=")" suffixOverrides=",">
             <if test="projectId != null">#{projectId},</if>
@@ -88,6 +93,7 @@
             <if test="overdueDate != null">#{overdueDate},</if>
             <if test="isSwitch != null">#{isSwitch},</if>
             <if test="authorityType != null">#{authorityType},</if>
+            <if test="macAddress != null">#{macAddress},</if>
         </trim>
     </insert>
 
@@ -100,6 +106,7 @@
             <if test="overdueDate != null">overdue_date = #{overdueDate},</if>
             <if test="isSwitch != null">is_switch = #{isSwitch},</if>
             <if test="authorityType != null">authority_type = #{authorityType},</if>
+            <if test="macAddress != null">mac_address = #{macAddress},</if>
         </trim>
         where id = #{id}
     </update>

+ 21 - 2
authority-ui/src/views/authority/secret/index.vue

@@ -133,6 +133,9 @@
             />
           </el-select>
         </el-form-item>
+        <el-form-item label="MAC地址" prop="macAddress" style="width: 360px">
+          <el-input v-model="form.macAddress" placeholder="请输入MAC地址"></el-input>
+        </el-form-item>
       </el-form>
       <div slot="footer" class="dialog-footer">
         <el-button type="primary" @click="submitForm">确 定</el-button>
@@ -196,7 +199,8 @@ export default {
         pageNum: 1,
         pageSize: 10,
         projectName: null,
-        secretKey: null
+        secretKey: null,
+        macAddress:null,
       },
       projectData: [],
       // 表单参数
@@ -209,7 +213,11 @@ export default {
       rules: {
         authorityType: [{required: true, message: "授权认证类型不能为空", trigger: "change"}],
         overdueDate: [{required: true, message: "过期时间不能为空", trigger: "change"}],
-        projectName: [{required: true, message: "项目名称不能为空", trigger: "change"}]
+        projectName: [{required: true, message: "项目名称不能为空", trigger: "change"}],
+        macAddress: [
+          { required: true, message: 'mac地址不能为空', trigger: 'change' },
+          { validator: this.validateMacAddress, trigger: 'change' }
+        ]
       },
     };
   },
@@ -218,6 +226,17 @@ export default {
     this.getProjectList()
   },
   methods: {
+    handleMacAddressChange() {
+      this.$refs.form.validateField('macAddress');
+    },
+    validateMacAddress(rule, value, callback) {
+      const macRegex = /^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$/;
+      if (!value || macRegex.test(value)) {
+        callback(); // 格式正确
+      } else {
+        callback(new Error('请输入有效的MAC地址')); // 格式错误
+      }
+    },
     // 任务组名字典翻译
     jobGroupFormat(row, column) {
       return this.selectDictLabel(this.dict.type.sys_is_status, row.authorityType);

+ 1 - 1
authority-ui/vue.config.js

@@ -35,7 +35,7 @@ module.exports = {
     proxy: {
       // detail: https://cli.vuejs.org/config/#devserver-proxy
       [process.env.VUE_APP_BASE_API]: {
-        target: `http://localhost:8080`,
+        target: `http://localhost:8002`,
         changeOrigin: true,
         pathRewrite: {
           ['^' + process.env.VUE_APP_BASE_API]: ''