Spring Boot - HTTPS で Web アプリを起動したらサブドメインの HTTP が開けなくなった

Spring Boot & Spring Security ベースの Web アプリケーションを
https://mydomain (SSL) で動かしたら、
http://sub.mydomain (サブドメインで非 SSL) にアクセスできなくなったときのメモ。

HSTS

原因は HSTS (HTTP Strict Transport Security) という仕組みでした。

Strict-Transport-Security ヘッダで includeSubDomains が指定されていたため、
サブドメインの方まで HTTPS にリダイレクトされたような動きをしていたようです。

対策

spring-boot-starter-security をそのまま使ってる場合は、
プロパティ (application.yml とか) で security.headers.hsts
設定してあげれば OK でした。

このへんの自動設定 で制御してる感じでした。

security.headers.hsts: DOMAIN
  # NONE  : "Strict-Transport-Security" ヘッダを吐かない。
  # DOMAIN: "includeSubDomains" を付けない。
  # ALL   : "includeSubDomains" を付ける。

WebSecurityConfigurerAdapter でカスタマイズしてる場合は、
こんなコードでいけました。

@Override
protected void configure(HttpSecurity http) throws Exception {
  http.headers().httpStrictTransportSecurity().includeSubDomains(false);
}

参考