JS/Java 传递 中文参数 编码 解码 前端encodeURIComponent默认不会对感叹号(!)进行编码 解决#号等特殊字符传输过程会被浏览器篡改的问题

JS/Java 传递 中文参数

一、JS传Java

//js端需编码3次

filePath = encodeURIComponent(filePath);

filePath = encodeURIComponent(filePath);

filePath = encodeURIComponent(filePath);

//java端解码2次

(Java端request.getParameter("name")之前会自动调用一次默认的ISO-8859-1解码)

queryString = URLDecoder.decode(queryString, "UTF-8");

queryString = URLDecoder.decode(queryString, "UTF-8");

一.零、JS传Java

JS端

shopName = encodeURIComponent(shopName);

Java端

shopName = URLDecoder.decode(shopName, "UTF-8");

二、Java传JS

Java端

returnVal = URLEncoder.encode(returnVal, "UTF-8");

//将加号(+)统一改为%20,解决JS端“空格变成加号(+)的问题”
returnVal = returnVal .replace("+", "%20");

JS端

returnVal = decodeURIComponent(returnVal);

注意:前端encodeURIComponent默认不会对感叹号(!)进行编码,【感叹号在URL编码中有特殊含义】,而后端Java的URLDecoder是完整编码

解决方案:

// like12 find bug,20240605,解决前端encodeURIComponent默认不会对感叹号(!)进行编码的问题【感叹号在URL编码中有特殊含义】
sysUser.passwordNew = sysUser.passwordNew.replace(/\!/g, '%21') // g表示全局替换

完整代码:

  /**
  * 修改密码
  */
  modifyPassword(sysUser) {
    // like12 find bug,20240605,解决前端encodeURIComponent默认不会对感叹号(!)进行编码的问题【感叹号在URL编码中有特殊含义】
    sysUser.password = sysUser.password.replace(/\!/g, '%21') // g表示全局替换
    sysUser.passwordNew = sysUser.passwordNew.replace(/\!/g, '%21') // g表示全局替换
    // 解决#号等特殊字符传输过程会被浏览器篡改的问题(如:ABCabc012@#)
    sysUser.password = encodeURIComponent(sysUser.password)
    sysUser.passwordNew = encodeURIComponent(sysUser.passwordNew)
    return request({
      url: `/${groupName}/modifyPassword`,
      method: 'post',
      data: sysUser
    })
  },
  /**
  * 设置密码
  */
  setPassword(sysUser) {
    // like12 find bug,20240605,解决前端encodeURIComponent默认不会对感叹号(!)进行编码的问题【感叹号在URL编码中有特殊含义】
    sysUser.passwordNew = sysUser.passwordNew.replace(/\!/g, '%21') // g表示全局替换
    // 解决#号等特殊字符传输过程会被浏览器篡改的问题(如:ABCabc012@#)
    sysUser.passwordNew = encodeURIComponent(sysUser.passwordNew)

    return request({
      url: `/${groupName}/setPassword`,
      method: 'post',
      data: sysUser
    })
  },
// 登录的后端接口配置
export function login(data) {
  // like12 find bug,20240605,解决前端encodeURIComponent默认不会对感叹号(!)进行编码的问题【感叹号在URL编码中有特殊含义】
  data.password = data.password.replace(/\!/g, '%21') // g表示全局替换
  // like12 add,20231207,登录接口禁止明文传输用户名密码,改为AES加密
  data.username = encryptTool.encrypt(data.username)
  data.password = encryptTool.encrypt(data.password)
  // like12 find bug,20230518,解决IE兼容模式时中文用户名无法登录的问题 后端接收到为乱码
  data.username = encodeURIComponent(data.username)
  data.password = encodeURIComponent(encodeURIComponent(data.password))
  /* // 原来是这样的
  // like12 find bug,20230518,解决IE兼容模式时中文用户名无法登录的问题 后端接收到为乱码
  data.username = encodeURIComponent(data.username) */

  return request({
    // like12 modified,20221004,拼接参数才行
    url: '/xxx/xxx?grant_type=password&username=' + data.username + '&password=' + data.password,
    method: 'post',
    data
  })
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值