Explorar o código

开启参数加密

lifanagju_citu hai 6 meses
pai
achega
9ae3cbc2d3
Modificáronse 2 ficheiros con 30 adicións e 0 borrados
  1. 1 0
      package.json
  2. 29 0
      src/config/axios/service.js

+ 1 - 0
package.json

@@ -20,6 +20,7 @@
     "html2canvas": "^1.4.1",
     "js-base64": "^3.7.7",
     "js-cookie": "^3.0.5",
+    "js-sha256": "^0.11.0",
     "lodash": "^4.17.21",
     "nprogress": "^0.2.0",
     "pinia": "^2.1.7",

+ 29 - 0
src/config/axios/service.js

@@ -11,6 +11,8 @@ import { getToken, getRefreshToken, setToken, setRefreshToken, getIsEnterprise }
 import { rewardEventTrackClick } from '@/api/integral'
 import errorCode from './errorCode'
 import { useI18n } from '@/hooks/web/useI18n'
+import { generateUUID } from "@/utils/index" 
+import { sha256 } from 'js-sha256'
 
 // import { resetRouter } from '@/router'
 // import { deleteUserCache } from '@/hooks/web/useCache'
@@ -74,6 +76,33 @@ service.interceptors.request.use(
     if (getToken(tokenIndex) && !isToken) {
       (config).headers.Authorization = 'Bearer ' + getToken(tokenIndex) // 让每个请求携带自定义token
     }
+
+    // 开启参数加密
+    if (config.openEncryption) {
+      // 1.请求的参数json // 携带的json一下,url拼接参数的直接用,没有参数不拼接
+      const jsonData = config.data ? JSON.stringify({...config.data}) :
+        config.params ? qs.stringify({...config.params}, { allowDots: true }) :
+        config.url.split('?')?.length>1 ? config.url.split('?')[1] : ''
+      // 2.固定的参数初始化成字符串
+      const supHeaders = {
+        appId: 'test', // 与后端协商一致使用
+        nonce: generateUUID(), // 前端生成唯一参数
+        timestamp: new Date().getTime(),
+      }
+      const appSecret = '123456' // 与后端协商一致使用
+      const staticHeadersStr = Object.keys(supHeaders).reduce((str, key) => {
+        str = str ? str + `&${key}=${supHeaders[key]}` : `${key}=${supHeaders[key]}`
+        return str
+      }, '') + appSecret
+
+      // 3.固定参数和请求参数拼接(拼接1和2)// signString
+      const finalStr = jsonData + staticHeadersStr
+      // 加密字符串
+      if (finalStr) supHeaders.sign = sha256(finalStr)
+      // 4.请求头加参数: appId + nonce + timestamp + sign // 顺序不能变
+      if (supHeaders) Object.keys(supHeaders).forEach(key => { (config).headers[key] = supHeaders[key] })
+    }
+
     // 设置租户
     if (tenantEnable && tenantEnable === 'true') {
       const tenantId = import.meta.env.VITE_TENANTCODE