对于最新的稳定版本,请使用 Spring Security 6.3.1Spring中文文档

对于最新的稳定版本,请使用 Spring Security 6.3.1Spring中文文档

所有基于 HTTP 的通信都应使用 TLS 进行保护。Spring中文文档

您可以在下面找到有关有助于使用 HTTPS 的 Servlet 特定功能的详细信息。Spring中文文档

重定向至HTTPS

如果客户端使用 HTTP 而不是 HTTPS 发出请求,则可以将 Spring Security 配置为重定向到 HTTPS。Spring中文文档

例如,以下 Java 配置会将所有 HTTP 请求重定向到 HTTPS:Spring中文文档

重定向至HTTPS
@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 请求重定向到 HTTPSSpring中文文档

重定向至HTTPS与XML配置
<http>
	<intercept-url pattern="/**" access="ROLE_USER" requires-channel="https"/>
...
</http>

严格的运输安全

Spring Security 提供对严格传输安全性的支持,并在默认情况下启用它。Spring中文文档

代理服务器配置