英文:
How to disable Keep alive in SpringBoot application in docker image
问题
我们有一个Web应用程序,使用以下技术:Java,SpringBoot,Docker,微服务,Jhipster。
前端容器的端口号是80。
我正在尝试禁用前端微服务的保持活动(keep-alive)选项,因为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)选项仍然启用:
连接到qwerty.xyz|10.10.219.200|:80... 已连接。
已发送HTTP请求,正在等待响应...
HTTP/1.1 200 OK
日期:周一,2020年4月6日21:14:50 GMT
服务器:Apache
上次修改时间:周五,2016年10月21日08:42:15 GMT
ETag:"4107-84-53f5c04e1c7c0"
接受范围:字节
内容长度:132
缓存控制:max-age=0,no-store
保持活动(keep-alive):timeout=15,max=100
连接:保持活动(keep-alive)
内容类型:文本/ HTML
长度:132 [文本/ HTML]
保存到:`/dev/null'
0K 100% 15.7M=0s
2020年4月6日23:14:50(15.7 MB/s)- `/dev/null' 已保存[132/132]
我的Spring Boot配置:
<!-- 依赖版本 -->
<jhipster-dependencies.version>3.0.1</jhipster-dependencies.version>
<!-- spring-boot版本应与由https://mvnrepository.com/artifact/io.github.jhipster/jhipster-dependencies/${jhipster-dependencies.version} 管理的版本匹配 -->
<spring-boot.version>2.1.4.RELEASE</spring-boot.version>
请问您可以帮我禁用它吗?
英文:
We have a Web application with following technologies: Java, SpringBoot, Docker, Microservices, Jhipster.
The port number for the frontend container is 80.
I am trying to disable keep alive option for the frontend microservice because SSO Authentication Server requires this parameter set to be false.
I tried to create the front container with maven : mvn -Pqpm,no-liquibase -Dhttp.keepAlive=false clean verify jib:dockerBuild
I also tried to disable in pom.xml of the front container :
<http.keepAlive>false</http.keepAlive>
<https.keepAlive>false</https.keepAlive>
But the keep-alive option remains enabled when I send a http request :
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]
My Springboot config:
<!-- 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>
Could you please help me to disable it ?
答案1
得分: 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;
...
// 其他属性
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf()
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
.and()
.addFilterBefore(corsFilter, CsrfFilter.class)
.addFilterBefore(keepAliveFilter, CsrfFilter.class)
;
}
英文:
I finally found a solution. The way we did this was to implement a filter and add this filter to SecurityConfig.config method.
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)
;
}
答案2
得分: 0
你可以在你的 Spring application.properties 文件中禁用它,对于我的情况,我正在使用一个 yaml 配置文件,所以在我的 application.yml 文件中进行如下配置:
server:
undertow:
always-set-keep-alive: false
英文:
You can disable it in your spring application.properties file, for my case I'm using a yaml config file, so in my application.yml file I did:
server:
undertow:
always-set-keep-alive: false
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论