From 670d895ebdedd89861fc0177be703f026b3ce625 Mon Sep 17 00:00:00 2001 From: hejianhao <15708179461@qq.com> Date: 星期四, 08 五月 2025 15:14:33 +0800 Subject: [PATCH] 接口加密 --- laboratory/src/utils/request.js | 38 ++++++++++++++++++++++++++------------ 1 files changed, 26 insertions(+), 12 deletions(-) diff --git a/laboratory/src/utils/request.js b/laboratory/src/utils/request.js index 112b18c..f1d1c11 100644 --- a/laboratory/src/utils/request.js +++ b/laboratory/src/utils/request.js @@ -1,13 +1,10 @@ import axios from 'axios' import apiConfig from './baseurl' - -import { - Message -} from 'element-ui' - +import { Message } from 'element-ui' +import { encryptBySM4, decryptBySM4 } from './sm4' // 添加decryptBySM4 const service = axios.create({ - baseURL: apiConfig.baseURL, + // baseURL: apiConfig.baseURL, withCredentials: false, // 当跨域请求时发送cookie timeout: 30000, // request timeout }) @@ -16,6 +13,10 @@ service.interceptors.request.use( config => { config['headers']['Authorization'] = `${sessionStorage.getItem('token')}` + + // 判断是否需要加密(只对/api开头的请求进行加密) + const needEncrypt = config.url.startsWith('/api'); + if (config.method == 'get') { if (!config.params) config.params = {}; config.params = { @@ -24,8 +25,8 @@ } if (config.method == 'post') { if (!config.data) config.data = {}; - config.data = { - ...config.data, + if (needEncrypt) { + config.data = { param: encryptBySM4(config.data) }; } } return config @@ -42,11 +43,24 @@ return } const res = response; - if (res.data.code == 200) { - if (!res.data.data) { - return Promise.resolve({...res.data}) + + // 新增解密处理:仅处理/api路径的POST响应 + if (res.config.method === 'post' && res.config.url.startsWith('/api')) { + try { + if (res.data && res.data.data) { + // 这里假设使用decryptBySM4进行解密 + res.data.data = decryptBySM4(res.data.data); + } + } catch (e) { + console.error('数据解密失败:', e); } - return Promise.resolve(res.data.data) + } + + if (res.data.code == 200) { + if (!res.data) { + return Promise.resolve({}) + } + return Promise.resolve(res.data) } else { if (res.data.code == 103 || res.data.code == 401) { Message({ -- Gitblit v1.7.1