axios使用sm2加密数据后请求参数多了双引号解决方法

axios使用sm2加密数据后请求参数多了双引号解决

背景

因项目安全要求,需对传给后端的入参加密,将请求参数加密后再传给后端

前期将axios降低到1.6.7后解决了问题,但最近axios有漏洞,安全要求对版本升级,问题需要继续解决。

果然,技术栈不解决迟早会有问题。。。

问题描述

升到稳定版本后,在浏览器网络请求参数上发现加密后的参数加上了双引号

后端的日志也能看出来多了双引号,导致后端解密失败

正常的请求参数是不带双引号的

解决过程

  1. 一开始想将content-type转为text传给后端,但是此路不通,会导致后端无法接收参数,只能传json格式

    Content-Type: application/json

  2. 那就转换思路,看axios是在什么地方将字符串添加了双引号的,于是在github上找到下面代码,解决问题
    代码段来自于别人的提交记录,亲测有效

    const instance = axios.create({
    transformRequest: function transformRequest(data, headers) {
    // doesn't apply the default transformRequest if the data is a string, so that axios doesn't add quotes see :
    // https://github.com/usebruno/bruno/issues/2043
    // https://github.com/axios/axios/issues/4034
    const hasJSONContentType = () => {
    const contentType = (headers && headers['Content-Type']) || '';
    return contentType.indexOf('application/json') > -1;
    };
    if (typeof data === 'string' && hasJSONContentType()) {
    return data;
    }

       axios.defaults.transformRequest.forEach((tr) => tr(data, headers));
     }
    

    });

相关细节可查看

https://github.com/axios/axios/issues/4034

https://github.com/usebruno/bruno/pull/2449

相关推荐
wuming先生4 天前
关于axios同步获取数据的问题
axios·web·异步网络
爱桥代码的程序媛7 天前
HarmonyOS开发5.0【封装request泛型方法】axios
axios·harmonyos·鸿蒙·鸿蒙系统·openharmony·封装·泛型
readmancynn7 天前
axios
前端·axios
营赢盈英14 天前
OpenAI transcription API bad request
javascript·ai·node.js·whisper·axios·openai api
闲人编程16 天前
Python 实现 SHA-1 数字摘要签名算法
开发语言·python·算法·密码学·加解密·数字签名·sha
一雨方知深秋18 天前
axios响应
开发语言·前端·javascript·数据库·vue.js·axios
洛*璃19 天前
Vue3 + Axios双Token刷新解决方案
前端·javascript·vue.js·axios·双token刷新
Heaven64520 天前
Chapter 06 axios使用指南
前端·vue.js·vue·axios
一雨方知深秋20 天前
axios
前端·javascript·vue.js·github·axios
秋窗71 个月前
今日总结:axios Get方法/Post方法参数传递方式对比
axios·总结