java - 如何在 docker 镜像中禁用 SpringBoot 应用程序中的 Keep alive
问题描述
我们有一个使用以下技术的 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>
你能帮我禁用它吗?
解决方案
我终于找到了解决方案。我们这样做的方法是实现一个过滤器并将这个过滤器添加到 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)
;
}
推荐阅读
- python - 静音和删除邮件反垃圾邮件 discord.py
- r - 如何从 R 闪亮的应用程序制作 pdf 报告
- reactjs - 如何将 Effector 与 Class 组件一起使用
- swift - 使用 Swift 在 Firestore 中的快照侦听器范围
- python - 可以在 openvino 中运行演示声音分类应用程序
- amazon-web-services - AWS SES 接口终端节点和 Lambda
- javascript - Javascript合并数组中的多个对象
- javascript - 使用纯 JavaScript 显示加载器
- oracle - 如何将 dba_users 表与 HRMS oracle 表连接起来
- node.js - 只有超级管理员可以将用户添加到日历的 Acl 吗?