HTTP
所有基于 HTTP 的通信都应使用 TLS进行保护。
本节讨论有助于使用 HTTPS 的特定于 Servlet 的功能的详细信息。
重定向到 HTTPS
如果客户端使用 HTTP 而不是 HTTPS 发出请求,您可以配置 Spring Security 将其重定向到 HTTPS。
例如,以下 Java 或 Kotlin 配置会将任何 HTTP 请求重定向到 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
<http>
<intercept-url pattern="/**" access="ROLE_USER" requires-channel="https"/>
...
</http>
严格传输安全
Spring Security 提供了对Strict Transport Security的支持,并且默认启用它。
代理服务器配置
Spring Security 与代理服务器集成。