java - 有没有办法在 Spring boot 中获取原始的 post body?
问题描述
我正在尝试验证 spring boot 2.1 中的松弛请求。我可以在单元测试中正确计算哈希。但是,当服务器处理实际请求时,我无法进行验证。这是因为 url 参数的顺序在初始请求和调用控制器的时间之间发生了变化。
您可以使用下面的示例控制器和 curl 重现此内容。请注意,curl 中的表单有效负载是hello=world&foo=baz&two=three
并且响应被重新排序为two=three&foo=baz&hello=world
.
理论
我相信这正在发生。Spring 检测到表单数据已经发布,然后对表单参数进行编码,并将它们写入新的输入流。似乎迭代以对参数进行编码的映射不保留原始顺序。
问题
有没有办法获得原始的帖子正文?
复制
@Controller
@ResponseBody
public class SlackController {
@RequestMapping(
path = "slack/command",
method = RequestMethod.POST,
produces = { MediaType.TEXT_PLAIN_VALUE },
consumes = { MediaType.APPLICATION_FORM_URLENCODED_VALUE })
public String executeCommand(
@RequestBody String requestBody
) {
return requestBody;
}
}
$ curl -X POST 'http://localhost:8080/slack/command' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-H 'Accept: text/plain' \
-d 'hello=world&foo=baz&two=three'
HTTP/1.1 200 OK
Date: Thu, 25 Apr 2019 05:26:53 GMT
Content-Type: application/json;charset=iso-8859-1
Content-Length: 29
two=three&foo=baz&hello=world
解决方案
推荐阅读
- c++ - 竞争条件统计和 mkdir
- firebase - 我们可以在firebase中创建一个父节点的ArrayList吗
- java - 在 Java 中过滤和合并 2 个 HashMap
- scala - Scala Akka Typed - pipeToSelf
- python - 使用 Fastai 将一维张量转换为一维数组
- ios - 无法解释的崩溃:NSInvalidArgumentException,多次推送同一个视图控制器实例
- google-analytics - 是否有测试 Google Analytics(分析)人口统计数据收集的方法?
- python - C++ 共享库:使用 Python CTypes 调用散列函数时不返回任何值
- javascript - 警告:失败的道具类型:道具“历史”在“路由器”中标记为必需,但其值为“未定义”。反应路由器dom v^5.2.0
- python - Pandas Python 通过从另一个数据帧的行中选择系数来乘以列值