最新の安定バージョンについては、Spring Security 6.3.1 を使用してください!

HTTP

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

以下に、HTTPS の使用を支援する WebFlux 固有の機能に関する詳細を示します。

HTTPS にリダイレクト

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

例: 次の 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 をサポートし、デフォルトで有効にします。

プロキシサーバー構成