The [+] plus sign in the request parameter is missing problem solved

Recently, when testing the interface, it appeared that the signature was sent through postman request, and the [+] sign in the signature was lost when received at the back end, which caused the verification to fail. After checking by the boss, I found the [+] sign in the signature Lost, record it here

1. When the postman requests, the [+] sign in the signature will be lost
2. The client requests, the [+] in the signature is not lost, and the code can not be processed at this time
3. What if the client requests normally deal with:

The cause of the problem: URLDecoder is caused, the following is URLDecoder's documentation:
The following rules are applied in the conversion : 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
following part is the key point: [+] sign will be replaced with a space!
The plus sign "+" is converted into a space character "".
As mentioned above, the alphanumeric characters "a" to "z", "A" to "Z", and "0" to "9" remain unchanged.
The special characters ".", "-", "* ","
"will remain unchanged, but the [+] will be replaced with a space!

Spring mvc框架在给参数赋值的时候调用了URLDecoder,如何解决这个问题,我们需要在请求的时候对"+"做处理:
String plusEncode = URLEncoder.encode("+", "UTF-8");
param = param.replaceAll("\\+", plusEncode);
这里在请求发送前,将加号用URLEcoder进行编码,并将参数json中的所有加号替换成编码后的字符。

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_42258975/article/details/108593999
Recommended