HTTP
所有基于 HTTP 的通信都应 使用 TLS 进行保护。
本节讨论了有助于使用 HTTPS 的 Servlet 特定功能的详细信息。
重定向到 HTTPS
如果客户端使用 HTTP 而非 HTTPS 发起请求,可以配置 Spring Security 以重定向到 HTTPS。
例如,以下 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,并默认启用它。
代理服务器配置
Spring Security 集成了代理服务器。