问题场景
用户支付后,跳转失败展示也,实际用户已扣款,webhook 通知支付成功
问题原因分析
// 用户支付成功后,returnUrl 跳转到后端地址同时带参数
curl --location --request POST 'http://localhost:8080/api/v1/returnUrl?key1=1&key2=3' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'key1=2' \
--data-urlencode 'key2=3'
HttpSerletRequest.getParameterMap("key1") // 结果1,2
从参数来看,urlParam 和form body 中有相同的key
Servlet是这样处理的:
HttpSerletRequest.getParameterMap 会判断 body 的stream 是否被读取,
读取过,就直接返回queryUrlParam
没有读取过,queryUrlParam 和 formParam 会合并
思考
Servlet 可能考虑HttpSerletRequest.getParameterMap 会出现重复读取数据的问题,在读取过stream后会,防止被重复读取数据,HttpSerletRequest.getParameterMap 不会包含body 中的数据
源码


解决方案
- 只读取queryUrlParam(建议此种方式)
- 代码中判断是否包含‘,’ 进行分割取第一个(有一定风险,如果value中包含’,’)