首页 > 解决方案 > 有没有办法在 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

标签: javaspringspring-bootslackslack-commands

解决方案


推荐阅读