springboot请求头key被小写

先说结论:springboot 2.2 ,引用的tomcat9,Http11InputBuffer 类会将head中key转小写。

解决:在项目中新建Http11InputBuffer类,保持和jar包的包路径一致,注释转小写的代码。

@PostMapping("head/{id}.json")
public String head(@RequestHeader Map<String, String> head, @PathVariable int id, @RequestBody User user, HttpServletRequest request, HttpServletResponse response) {
    response.setHeader("storeId","11002054");    
    if (head.containsKey("storeId")) {
        return head.get("storeId");
    }  
    return user.getUserName();
}

简单测试http头部信息。

调试代码,先查看了HttpServlet方法中req参数

protected void service(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {...}

这里getHeaders(),发现key已经是小写了。往上一层级追踪,应该是在tomcat接收请求时,查看最早的连接参数。

Http11Processor 类

if (!inputBuffer.parseHeaders()) {
    // We've read part of the request, don't recycle it
    // instead associate it with the socket
    openSocket = true;
    readComplete = false;
    break;
}

tomcat在此处解析了头信息

Http11InputBuffer类
boolean parseHeaders() throws IOException {
    if (!parsingHeader) {
        throw new IllegalStateException(sm.getString("iib.parseheaders.ise.error"));
    }

    HeaderParseStatus status = HeaderParseStatus.HAVE_MORE_HEADERS;

    do {
        status = parseHeader();//这里执行解析

        ....   

       }while (status == HeaderParseStatus.HAVE_MORE_HEADERS);

}

private HeaderParseStatus parseHeader() throws IOException {
// chr is next byte of header name. Convert to lowercase.
if ((chr >= Constants.A) && (chr <= Constants.Z)) {
    byteBuffer.put(pos, (byte) (chr - Constants.LC_OFFSET));
}

}

这段代码在   Http11InputBuffer   829-831 行

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值