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

HTTP

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

このセクションでは、HTTPS の使用を支援するサーブレット固有の機能の詳細について説明します。

HTTPS にリダイレクト

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

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

HTTPS にリダイレクト
  • Java

  • Kotlin

@Configuration
@EnableWebSecurity
public class WebSecurityConfig {

	@Bean
	public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
		http
			// ...
			.requiresChannel(channel -> channel
				.anyRequest().requiresSecure()
			);
		return http.build();
	}
}
@Configuration
@EnableWebSecurity
class SecurityConfig {

    @Bean
    open fun filterChain(http: HttpSecurity): SecurityFilterChain {
        http {
            // ...
            requiresChannel {
                secure(AnyRequestMatcher.INSTANCE, "REQUIRES_SECURE_CHANNEL")
            }
        }
        return http.build()
    }
}

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

XML 設定で HTTPS にリダイレクトする
<http>
	<intercept-url pattern="/**" access="ROLE_USER" requires-channel="https"/>
...
</http>

Strict Transport Security

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

プロキシサーバー構成