请求参数为加密过后的密码,参数中的“+“号转变为“ ”关于URLDecode的问题

       昨天遇到一个问题是.NET通过加密算法对密码进行加密,我后台接收的时候接收参数,接口功能为手机号+密码登录,但是其中一个人的密码一直登录不上,我这边返回的信息是输入账号或者密码不正确,要么手机号不对,要么密码不对,仔细看完手机号对应数据库是正确的,后续debug,发现接收到的参数“Sc0Jh8e8s+g=” 变成了“Sc0Jh8e8s g=”

“+”号丢失,后续查阅资料也找到的原因。

URLDecode官方解读为:

The following rules are applied in the conversion:

  • The alphanumeric characters "a" through "z", "A" through "Z" and "0" through "9" remain the same.
  • The special characters ".", "-", "*", and "_" remain the same.
  • The plus sign "+" is converted into a space character "   " .
  • A sequence of the form "%xy" will be treated as representing a byte where xy is the two-digit hexadecimal representation of the 8 bits. Then, all substrings that contain one or more of these byte sequences consecutively will be replaced by the character(s) whose encoding would result in those consecutive bytes. The encoding scheme used to decode these characters may be specified, or if unspecified, the default encoding of the platform will be used

 翻译:

以下规则适用于转换:

字母数字字符“a”到“z”,“a”到“z”,“0”到“9”保持不变。
特殊字符“。”、“-”、“*”和“_”保持不变。
加号“+”被转换为空格字符“ ”。
形式为“%xy”的序列将被视为表示一个字节,其中xy是8位的两位数十六进制表示。
然后,所有连续包含一个或多个字节序列的子字符串将被其编码将产生这些连续字节的字符替换。
可以指定用于解码这些字符的编码方案,如果未指定,则使用平台的默认编码

 就是这样“加号“+”被转换为空格字符“ ”

Spring mvc框架在给参数赋值的时候调用了URLDecoder,那要解决这个问题,需要在请求的时候对"+"做处理:

我的解决办法是对密码进行二级加密:

后续.NET传参时取出密码将密码进行base64加密。

我这边接收参数再讲密码解密。得到和数据库相同信息的参数。

Controller调用ParameterUtil.getDecode

         //decode解码
        //接收到的参数为base64加密,base64部分密码会加密成带有"+"号的但是springMVC会调用    URLDecode,会将“+”号解析为空格
        //需要二级加密
        String decode = ParameterUtil.getDecode(param.getPassword());

 ParameterUtil.getDecode方法

/**
	 * 
	 * @param text
	 * @return
	 * @throws UnsupportedEncodingException 
	 * @throws Exception
	 */
	public static String getDecode(String text) throws UnsupportedEncodingException{
		Base64 base64 = new Base64();
		return new String(base64.decode(text), "UTF-8");

测试效果

解析成功

完美,nice,溜

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值