【问题标题】:Enable HSTS with Spring Boot does not work使用 Spring Boot 启用 HSTS 不起作用
【发布时间】:2022-01-18 13:00:07
【问题描述】:
  • 我有一个 Spring Boot 应用程序
  • 我想启用 HSTS

我将记录在案的设置添加到我的 SecurityConfiguration(见下文), 但没有出现 HSTS 标头。

我做错了什么?

@Slf4j
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.headers()
                .httpStrictTransportSecurity()
                .includeSubDomains(true)
                .preload(false)
                .maxAgeInSeconds(31536000);

在访问页面或在官方网站https://gf.dev/hsts-test进行测试时,我在 Chrome 开发者工具(F12 -> Network -> Headers -> Response Header)中找不到标题

【问题讨论】:

  • 你的后端使用https吗?
  • Spring Boot 应用程序在 http 8080 下运行,但 Dispatchserver 运行 https 将请求路由到 Spring Boot。我看到了http的问题。有没有办法告诉 Spring boot 在 http 的情况下也设置 HSTS Header?

标签: spring-boot spring-security hsts


【解决方案1】:

默认情况下,Spring Security 仅在连接安全 (https) 时才添加 HSTS 标头。请参阅documentation

但是,您可以根据需要更改RequestMatcher,如下所示:

http.headers()
                .httpStrictTransportSecurity()
                .includeSubDomains(true)
                .preload(false)
                .maxAgeInSeconds(31536000)
                .requestMatcher(AnyRequestMatcher.INSTANCE);

【讨论】:

    猜你喜欢
    • 2021-07-15
    • 2017-06-17
    • 1970-01-01
    • 2019-06-26
    • 2018-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多