HTTP

すべての HTTP ベースの通信は、TLS を使用して保護する必要があります。

このセクションでは、HTTPS の使用を支援する WebFlux 固有の機能の使用について詳しく説明します。

HTTPS にリダイレクト

クライアントが HTTPS ではなく HTTP を使用してリクエストを行う場合、HTTPS にリダイレクトするように Spring Security を構成できます。

次の Java 構成は、HTTP リクエストを HTTPS にリダイレクトします。

HTTPS にリダイレクト
  • Java

  • Kotlin

@Bean
SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
	http
		// ...
		.redirectToHttps(withDefaults());
	return http.build();
}
@Bean
fun springSecurityFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
    return http {
        // ...
        redirectToHttps { }
    }
}

構成をラップして、本番環境でのみオンにする if ステートメントをラップすることができます。または、本番環境でのみ発生するリクエストに関するプロパティを検索して有効にすることもできます。例: 本番環境で X-Forwarded-Proto という名前のヘッダーが追加されている場合は、次の Java 構成を使用する必要があります。

X-Forwarded 時に HTTPS にリダイレクトする
  • Java

  • Kotlin

@Bean
SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
	http
		// ...
		.redirectToHttps(redirect -> redirect
			.httpsRedirectWhen(e -> e.getRequest().getHeaders().containsKey("X-Forwarded-Proto"))
		);
	return http.build();
}
@Bean
fun springSecurityFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
    return http {
        // ...
        redirectToHttps {
            httpsRedirectWhen {
                it.request.headers.containsKey("X-Forwarded-Proto")
            }
        }
    }
}

Strict Transport Security

Spring Security は Strict Transport Security をサポートし、デフォルトで有効にします。

プロキシサーバー構成