如何在 spring-boot 3.x 中设置 maxHttpHeaderSize

per*_*ero 6 java spring spring-boot embedded-tomcat tomcat10

如Spring Boot 3 迁移指南中所述,server.max -http-header-size属性已被弃用。您可以使用server.max-http-request-header-size属性设置最大 http 请求标头大小。

我收到以下异常:

org.apache.coyote.http11.HeadersTooLargeException: An attempt was made to write 
more data to the response headers than there was room available in the buffer. 
Increase maxHttpHeaderSize on the connector 
or write less data into the response headers.
Run Code Online (Sandbox Code Playgroud)

我需要增加嵌入式 Tomcat 10 中的最大 http 响应标头大小。Tomcat 10支持maxHttpHeaderSizemaxHttpResponseHeaderSize属性。

我如何在 Spring Boot 3.x 中使用 来设置这些WebServerFactoryCustomizer

Sco*_*ick 7

您可以按照Spring Boot 文档WebServerFactoryCustomer中所示创建一个。

对于您的用例,这看起来像这样:

    @Component
    public class TomcatCustomizer implements WebServerFactoryCustomizer<TomcatServletWebServerFactory> {

        @Override
        public void customize(TomcatServletWebServerFactory server) {
            server.addConnectorCustomizers((connector) -> {
                ProtocolHandler handler = connector.getProtocolHandler();
                if (handler instanceof AbstractHttp11Protocol<?> protocol) {
                    protocol.setMaxHttpResponseHeaderSize(x);
                }
            });
        }

    }
Run Code Online (Sandbox Code Playgroud)

Spring Boot 3.1.0 将引入一个属性server.tomcat.maxHttpResponseHeaderSize来使其更容易配置。