【问题标题】:How to disable Keep alive in SpringBoot application in docker image如何在 Docker 映像中的 Spring Boot 应用程序中禁用 Keep alive
【发布时间】:2020-07-19 00:06:30
【问题描述】:

我们有一个使用以下技术的 Web 应用程序:Java、SpringBoot、Docker、微服务、Jhipster。 前端容器的端口号是 80。

我正在尝试禁用前端微服务的保持活动选项,因为 SSO 身份验证服务器要求将此参数设置为 false。

我尝试使用 maven 创建前端容器:mvn -Pqpm,no-liquibase -Dhttp.keepAlive=false clean verify jib:dockerBuild

我也尝试在前端容器的 pom.xml 中禁用:

<http.keepAlive>false</http.keepAlive>
<https.keepAlive>false</https.keepAlive>

但当我发送 http 请求时,keep-alive 选项仍然启用:

Connecting to qwerty.xyz|10.10.219.200|:80... connected.
HTTP request sent, awaiting response... 
  HTTP/1.1 200 OK
  Date: Mon, 06 Apr 2020 21:14:50 GMT
  Server: Apache
  Last-Modified: Fri, 21 Oct 2016 08:42:15 GMT
  ETag: "4107-84-53f5c04e1c7c0"
  Accept-Ranges: bytes
  Content-Length: 132
  Cache-Control: max-age=0, no-store
  Keep-Alive: timeout=15, max=100
  Connection: Keep-Alive
  Content-Type: text/html
Length: 132 [text/html]
Saving to: `/dev/null'

     0K                                                       100% 15.7M=0s

2020-04-06 23:14:50 (15.7 MB/s) - `/dev/null' saved [132/132]

我的 Springboot 配置:

<!-- Dependency versions -->
<jhipster-dependencies.version>3.0.1</jhipster-dependencies.version>
<!-- The spring-boot version should match the one managed by
https://mvnrepository.com/artifact/io.github.jhipster/jhipster-dependencies/${jhipster-dependencies.version} -->
<spring-boot.version>2.1.4.RELEASE</spring-boot.version>

你能帮我禁用它吗?

【问题讨论】:

  • 您使用哪个http客户端发送请求?
  • 你有没有尝试在你的 application.yml 中这样做?检查docs.spring.io/spring-boot/docs/current/reference/html/… 中的可用属性,因为Jhipster 默认使用Undertow,我会尝试server.undertow.always-set-keep-alive。如果您在 Undertow 的文档中发现未作为应用程序属性公开的 Undertow 设置,您仍然可以通过代码设置它,请参阅github.com/spring-projects/spring-boot/issues/16077
  • 嗨@GaëlMarziou,你写的参数是未知的。我使用的是旧版本的 Spring boot 吗?我在帖子中添加了我的配置。
  • 是的,doc of Spring Boot 2.1.4 没有提到它,所以你可以将 Spring Boot 升级到 2.2.x,这并不难。否则,您可以使用 UndertowBuilderCustomizer 通过代码将 ALWAYS_SET_KEEP_ALIVE 设置为 false。
  • 这不起作用。我认为它是由 jhipster 配置密钥管理的,但我没有找到它。当应用程序作为 jar 文件托管在 docker 映像中时,我不知道如何禁用此 http 配置。

标签: java spring-boot docker jhipster keep-alive


【解决方案1】:

你可以在你的 spring application.properties 文件中禁用它,对于我来说,我使用的是 yaml 配置文件,所以在我的 application.yml 文件中我做了:

server:
  undertow:
    always-set-keep-alive: false

【讨论】:

    【解决方案2】:

    我终于找到了解决办法。我们这样做的方法是实现一个过滤器并将这个过滤器添加到 SecurityConfig.config 方法中。

        KeepAliveFilter.java
    
        public class KeepAliveFilter implements Filter {
            public void init(final FilterConfig filterConfig) { }
    
            public void destroy() { }
    
            public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException {
                final HttpServletResponse httpResponse = (HttpServletResponse) response;
                httpResponse.setHeader("Connection", "close");
                chain.doFilter(request, response);
            }
        }
    
        SecurityConfiguration.java
    
        private final KeepAliveFilter keepAliveFilter;
    
        public SecurityConfiguration(final KeepAliveFilter keepAliveFilter, ...) 
        {
            this.keepAliveFilter = keepAliveFilter;
        ...
        // other property
        }
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http
                .csrf()
                .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
                .and()
                .addFilterBefore(corsFilter, CsrfFilter.class)
                .addFilterBefore(keepAliveFilter, CsrfFilter.class)
                ;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-17
      • 2013-05-24
      • 1970-01-01
      • 1970-01-01
      • 2012-03-31
      • 2020-12-16
      • 2014-01-27
      相关资源
      最近更新 更多