Spring WebFlux内嵌Netty请求头过大异常:如何配置最大大小?
解决Spring WebFlux内嵌Netty的HTTP/2请求头大小限制问题
你遇到的Http2Exception$HeaderListSizeException是HTTP/2协议的请求头列表大小超出默认限制(8192字节)导致的。server.max-http-header-size是针对HTTP/1.1的配置,对HTTP/2不生效,需要单独配置Netty的HTTP/2参数。
配置方式一:通过配置文件设置
在application.yml中添加:
server: http2: enabled: true netty: http: h2: max-header-list-size: 65536 # 可根据需求调整大小,示例为64KB
或在application.properties中添加:
server.http2.enabled=true server.netty.http.h2.max-header-list-size=65536
配置方式二:编程式自定义配置
如果需要更灵活的控制,可以创建自定义的Netty服务器配置Bean:
import org.springframework.boot.web.embedded.netty.NettyServerCustomizer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import reactor.netty.http.server.HttpServer; @Configuration public class NettyHttp2HeaderConfig { @Bean public NettyServerCustomizer http2MaxHeaderListSizeCustomizer() { return httpServer -> httpServer .http2Settings(settings -> settings.maxHeaderListSize(65536)); } }
注意事项
- 确保HTTP/2已启用(
server.http2.enabled=true),若为HTTP/1.1请求,仍需使用server.max-http-header-size配置。 - 调整
max-header-list-size时,需结合实际业务场景,避免设置过大引发安全风险。
内容的提问来源于stack exchange,提问作者Pawan Patil
相关产品推荐
相关产品推荐

