gpt4 book ai didi

java - Spring 严格传输安全(HSTS)配置不起作用

转载 作者:行者123 更新时间:2023-12-02 03:17:07 29 4
gpt4 key购买 nike

我正在尝试在我的 Spring Boot 应用程序中启用 HSTS。我已将以下内容添加到我的 WebSecurityConfig (基于 Enable HTTP Strict Transport Security (HSTS) with spring boot application ):

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter
{
protected void configure(HttpSecurity http) throws Exception
{
// Other http configurations, e.g. authorizeRequests and CSRF
// ...

http.headers().httpStrictTransportSecurity()
.maxAgeInSeconds(Duration.ofDays(365L).getSeconds())
.includeSubDomains(true);
}
}

确实在 HTTPS 请求中获取 strict-transport-security header ,但 max-age 始终为 0:

strict-transport-security: max-age=0; includeSubDomains

如果我添加 Spring 配置,我会得到完全相同的 header ,所以看起来我的配置没有被拾取。它似乎特定于 HSTS 配置,因为其他配置,例如http.authorizeRequests() 正在工作。这似乎表明 HSTS 配置在某种程度上被覆盖,特别是考虑到 Spring's default最大年龄是一年。不过,我已经能够在我们的代码库中找到任何其他与 HSTS 相关的配置。

我还尝试在 o.springframework.s.c.a.w.c.HeadersConfigurer.HstsConfig#maxAgeInSeconds 中设置断点来检查它是否被多次调用。我从 configure 进行的调用是唯一的调用。

有谁知道为什么我的 HSTS 配置没有被使用?或者您对如何调试这个有任何其他想法吗?

最佳答案

事实证明,这是由 Cloudflare 配置导致的,该配置重写了 header 并将 max-age 设置为 0。

更新 Cloudflare 配置也比更新每个微服务中的配置更容易。

关于java - Spring 严格传输安全(HSTS)配置不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56955641/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com