hejianhao
2025-05-08 670d895ebdedd89861fc0177be703f026b3ce625
接口加密
4个文件已修改
2个文件已添加
95 ■■■■ 已修改文件
laboratory/package.json 3 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
laboratory/src/utils/baseurl.js 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
laboratory/src/utils/encryption.js 34 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
laboratory/src/utils/request.js 38 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
laboratory/src/utils/sm4.js 14 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
laboratory/vue.config.js 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
laboratory/package.json
@@ -3,7 +3,7 @@
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "serve:dev": "cross-env VUE_APP_ENV=development vue-cli-service serve --port 8087",
    "serve:dev": "cross-env VUE_APP_ENV=development vue-cli-service serve --port 8090",
    "serve:prod": "cross-env VUE_APP_ENV=production vue-cli-service serve",
    "build:dev": "cross-env VUE_APP_ENV=development vue-cli-service build",
    "build:prod": "cross-env VUE_APP_ENV=production vue-cli-service build",
@@ -16,6 +16,7 @@
    "core-js": "^3.41.0",
    "element-ui": "^2.15.6",
    "moment": "^2.30.1",
    "sm-crypto": "^0.3.13",
    "vue": "^2.7.16",
    "vue-router": "^3.6.5",
    "vuex": "^3.6.2"
laboratory/src/utils/baseurl.js
@@ -1,7 +1,7 @@
const apiConfig = {
    // 开发环境
    development: {
        baseURL: "http://192.168.110.34:8081/",
        baseURL: "http://192.168.110.34:8081",
        imgUrl: "",
    },
    // 生产环境
laboratory/src/utils/encryption.js
New file
@@ -0,0 +1,34 @@
import axios from 'axios'
import apiConfig from './baseurl'
let encryptionKey = '2022lab02ora12to' // 默认密钥
// 从接口获取密钥
export const fetchEncryptionKey = async () => {
    try {
        const response = await axios.get(`${apiConfig.baseURL}/api/system/getEncryptionKey`)
        if (response.data && response.data.code === 200) {
            // 转换为Buffer并验证字节长度
            const keyBuffer = Buffer.from(response.data.data, 'utf-8')
            if (keyBuffer.length !== 16) {
                console.warn('无效密钥长度,使用默认密钥')
                return encryptionKey // 保持原有密钥
            }
            // 存储原始字符串和Buffer两种格式
            encryptionKey = response.data.data
            return encryptionKey
        }
    } catch (error) {
        console.error('获取加密密钥失败:', error)
    }
}
// 新增方法获取Buffer格式的密钥
export const getEncryptionKeyBuffer = () => {
    return Buffer.from(encryptionKey, 'utf-8')
}
// 获取当前密钥(保持字符串格式)
export const getEncryptionKey = () => encryptionKey
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);
      }
      return Promise.resolve(res.data.data)
      } catch (e) {
        console.error('数据解密失败:', e);
      }
    }
    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({
laboratory/src/utils/sm4.js
New file
@@ -0,0 +1,14 @@
import { sm4 } from 'sm-crypto';
import { getEncryptionKey, fetchEncryptionKey, getEncryptionKeyBuffer } from './encryption';
// SM4加密函数
export const encryptBySM4 = (data) => {
    const key = getEncryptionKeyBuffer(); // 获取当前密钥
    return sm4.encrypt(JSON.stringify(data), key);
};
// SM4解密函数
export const decryptBySM4 = (data) => {
    const key = getEncryptionKeyBuffer(); // 获取当前密钥
    return JSON.parse(sm4.decrypt(data, key));
};
laboratory/vue.config.js
@@ -10,11 +10,11 @@
        disableHostCheck: true, //禁用主机检查 
        proxy: {
            "/api": { // 设置以什么前缀开头的请求用来代理
                target: "http://localhost:8080", //要访问的跨域的域名
                target: "http://192.168.110.34:8081", //要访问的跨域的域名
                secure: false, // 使用的是http协议则设置为false,https协议则设置为true
                changOrigin: true, //开启代理
                pathRewrite: {
                    "^/api": "",
                    "^/api": "/api",
                },
            },
        },