java 无法在Sping Boot 中设置Strict-Transport-Security标头

fcipmucu  于 2023-09-29  发布在  Java
关注(0)|答案(1)|浏览(125)

我正在尝试将Strict Transport Security头设置到我的Spring Webflow应用程序。
这是我写的设置响应头的代码

@EnableWebSecurity
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        
        http.headers()
            .httpStrictTransportSecurity()
            .includeSubDomains(true).maxAgeInSeconds(31536000).and()
            .cacheControl().disable()
            .frameOptions().disable();       
    }
}

现在,我正在检查URL的响应头,尽管其他两个头(cache-control和frame-options)正在设置,但我在任何地方都没有看到Strict Transport Security头。

谢谢你

cngwdvgl

cngwdvgl1#

HstsConfig中使用的默认RequestMatcher检查请求是否为HTTPS HttpServletRequest.isSecure()。如果由于某些原因它对你来说还不够,你可以使用另一个匹配器。
下面的代码确保在所有响应中设置Strict-Transport-Security头:

http.headers()
          .httpStrictTransportSecurity()
          .requestMatcher(AnyRequestMatcher.INSTANCE)
          ...

相关问题