ソースを参照

中台-接口申请-审批、审核
中台-接口申请-加密解密

limeng 2 年 前
コミット
528ab3a42a

+ 3 - 0
mybusiness/src/main/java/com/sooka/apply/domain/IntDetailed.java

@@ -31,6 +31,9 @@ public class IntDetailed extends BaseEntity
     /** 接口地址 */
     private String interfaceAddress;
 
+    /** 接口编码 */
+    private String code;
+
     /** secret_key */
     private String secretKey;
 

+ 5 - 10
mybusiness/src/main/java/com/sooka/apply/service/impl/IntRecordServiceImpl.java

@@ -1,17 +1,13 @@
 package com.sooka.apply.service.impl;
 
-import java.util.Arrays;
 import java.util.List;
 
-import com.sooka.apply.domain.IntBo;
 import com.sooka.common.core.text.Convert;
 import com.sooka.common.utils.DateUtils;
-import com.sooka.common.utils.StringUtils;
 import com.sooka.common.utils.uuid.IdUtils;
-import com.sooka.common.utils.uuid.UUID;
 import com.sooka.framework.util.ShiroUtils;
-import com.sooka.framework.web.domain.server.Sys;
 import com.sooka.system.domain.SysUser;
+import com.util.SecretKeyUtil;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import java.util.ArrayList;
@@ -103,7 +99,7 @@ public class IntRecordServiceImpl implements IIntRecordService
         /**添加后返回主表ID**/
         intRecordMapper.insertIntRecord(intRecord);
         /**添加子表**/
-        return insertIntDetailed(intRecord.getId(),intRecord.getIntDetailedList());
+        return insertIntDetailed(intRecord.getId(),intRecord.getDuration(),intRecord.getIntDetailedList());
     }
 
     /**
@@ -131,7 +127,7 @@ public class IntRecordServiceImpl implements IIntRecordService
         /**删除子表**/
         intRecordMapper.deleteIntDetailedByRecordIds(Convert.toStrArray(intRecord.getId()));
         /**添加子表**/
-        return insertIntDetailed(intRecord.getId(),intRecord.getIntDetailedList());
+        return insertIntDetailed(intRecord.getId(),intRecord.getDuration(),intRecord.getIntDetailedList());
     }
 
     /**
@@ -204,14 +200,13 @@ public class IntRecordServiceImpl implements IIntRecordService
      *
      * @param list 申请记录对象
      */
-    private int insertIntDetailed(String id, List<IntDetailed> list){
-        System.out.println(id);
+    private int insertIntDetailed(String id, String duration, List<IntDetailed> list){
         List<IntDetailed> list_ = new ArrayList<IntDetailed>();
         for (int i = 0; i < list.size(); i++) {
             IntDetailed intDetailed = new IntDetailed();
             intDetailed.setRecordId(id);
             intDetailed.setIntId(list.get(i).getIntId());
-            intDetailed.setSecretKey(IdUtils.fastSimpleUUID());
+            intDetailed.setSecretKey(SecretKeyUtil.AESEncode("3fc674da58",duration+"/"+IdUtils.fastSimpleUUID()));
             intDetailed.setPurpose(list.get(i).getPurpose());
             intDetailed.setFile(list.get(i).getFile());
             list_.add(intDetailed);

+ 116 - 0
mybusiness/src/main/java/com/util/SecretKeyUtil.java

@@ -0,0 +1,116 @@
+package com.util;
+
+
+import sun.misc.BASE64Encoder;
+
+import javax.crypto.Cipher;
+import javax.crypto.KeyGenerator;
+import javax.crypto.SecretKey;
+import javax.crypto.spec.SecretKeySpec;
+import java.security.SecureRandom;
+import sun.misc.BASE64Decoder;
+
+/**
+ * 秘钥生成器工具类
+ *
+ * @author lei_wang
+ */
+public class SecretKeyUtil {
+
+
+    /**
+     * 加密
+     * 1.构造**生成器
+     * 2.根据ecnodeRules规则初始化**生成器
+     * 3.产生**
+     * 4.创建和初始化密码器
+     * 5.内容加密
+     * 6.返回字符串
+     */
+    public static String AESEncode(String encodeRules, String content) {
+        try {
+            //1.构造**生成器,指定为AES算法,不区分大小写
+            KeyGenerator aes = KeyGenerator.getInstance("AES");
+            //2.根据ecnodeRules规则初始化**生成器
+            //生成一个128位的随机源,根据传入的字节数组
+            aes.init(128, new SecureRandom(encodeRules.getBytes()));
+            //3.产生原始对称**
+            SecretKey original_key = aes.generateKey();
+            //4.获得原始对称**的字节数组
+            byte[] raw = original_key.getEncoded();
+            //5.根据字节数组生成AES**
+            SecretKey key = new SecretKeySpec(raw, "AES");
+            //6.根据指定算法AES自成密码器
+            Cipher cipher = Cipher.getInstance("AES");
+            //7.初始化密码器,第一个参数为加密(Encrypt_mode)或者解密解密(Decrypt_mode)操作,第二个参数为使用的KEY
+            cipher.init(Cipher.ENCRYPT_MODE, key);
+            //8.获取加密内容的字节数组(这里要设置为utf-8)不然内容中如果有中文和英文混合中文就会解密为乱码
+            byte[] byte_encode = content.getBytes("utf-8");
+            //9.根据密码器的初始化方式--加密:将数据加密
+            byte[] byte_AES = cipher.doFinal(byte_encode);
+            //10.将加密后的数据转换为字符串
+            //这里用Base64Encoder中会找不到包
+            //解决办法:
+            //在项目的Build path中先移除JRE System Library,再添加库JRE System Library,重新编译后就一切正常了。
+            String AES_encode = new String(new BASE64Encoder().encode(byte_AES));
+            //11.将字符串返回
+            return AES_encode;
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        //如果有错就返加nulll
+        return null;
+    }
+
+    /**
+     * 解密
+     * 解密过程:
+     * 1.同加密1-4步
+     * 2.将加密后的字符串反纺成byte[]数组
+     * 3.将加密内容解密
+     */
+    public static String AESDncode(String encodeRules, String content) {
+        try {
+            //1.构造**生成器,指定为AES算法,不区分大小写
+            KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
+            //2.根据ecnodeRules规则初始化**生成器
+            //生成一个128位的随机源,根据传入的字节数组
+            keyGenerator.init(128, new SecureRandom(encodeRules.getBytes()));
+            //3.产生原始对称**
+            SecretKey original_key = keyGenerator.generateKey();
+            //4.获得原始对称**的字节数组
+            byte[] raw = original_key.getEncoded();
+            //5.根据字节数组生成AES**
+            SecretKey key = new SecretKeySpec(raw, "AES");
+            //6.根据指定算法AES自成密码器
+            Cipher cipher = Cipher.getInstance("AES");
+            //7.初始化密码器,第一个参数为加密(Encrypt_mode)或者解密(Decrypt_mode)操作,第二个参数为使用的KEY
+            cipher.init(Cipher.DECRYPT_MODE, key);
+            //8.将加密并编码后的内容解码成字节数组
+            byte[] byte_content = new BASE64Decoder().decodeBuffer(content);
+            /*
+             * 解密
+             */
+            byte[] byte_decode = cipher.doFinal(byte_content);
+            String AES_decode = new String(byte_decode, "utf-8");
+            return AES_decode;
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        //如果有错就返加nulll
+        return null;
+    }
+
+    public static void main(String[] args) {
+        String str = SecretKeyUtil.AESEncode("3fc674da58", "2022-10-20 - 2022-10-29/b29c94d72ee74c6d94b573d90020ea59");
+        /*
+         * 加密
+         */
+        System.out.println("加密后的密文是:" + str);
+
+        /*
+         * 解密
+         */
+        System.out.println("解密后的明文是:" + SecretKeyUtil.AESDncode("3fc674da58", str));
+    }
+}

+ 3 - 1
mybusiness/src/main/resources/mapper/apply/IntRecordMapper.xml

@@ -45,6 +45,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
         <result property="file"    column="file"    />
         <result property="interfaceName"    column="interface_name"    />
         <result property="interfaceAddress"    column="interface_address"    />
+        <result property="code"    column="code"    />
     </resultMap>
 
     <sql id="selectIntRecordVo">
@@ -120,7 +121,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
             b.purpose,
             b.file,
             c.interface_name,
-            c.interface_address
+            c.interface_address,
+            c.code
         FROM
             int_record a
         LEFT JOIN int_detailed b ON b.record_id = a.id

+ 6 - 1
mybusiness/src/main/resources/templates/apply/record/detail.html

@@ -197,9 +197,13 @@
             '				</div>' +
             '				<div class="form-group" style="display: none" name="secretKey">' +
             '					<label class="col-sm-2 control-label">秘钥</label>' +
-            '					<div class="col-sm-10">' +
+            '					<div class="col-sm-4">' +
             '						<input type="text" readonly class="form-control" disabled value="#secretKey#">' +
             '					</div>' +
+            '					<label class="col-sm-2 control-label">接口编码</label>' +
+            '					<div class="col-sm-4">' +
+            '						<input type="text" readonly class="form-control" disabled value="#code#">' +
+            '					</div>' +
             '				</div>' +
             '				<div class="form-group">' +
             '					<label class="col-sm-2 control-label">接口用途</label>' +
@@ -224,6 +228,7 @@
             docm = docm.replace(/#id#/g,data[i].intId);
             docm = docm.replace(/#subId#/g,data[i].subId);
             docm = docm.replace(/#secretKey#/g,data[i].secretKey);
+            docm = docm.replace(/#code#/g,data[i].code);
             docm = docm.replace(/#formId#/g,"sub-form"+num);
             docm = docm.replace(/#file_upload#/g,"file_upload"+num);
             $("#accordion").append(docm);