2019-11-26 05:49:51 +08:00
|
|
|
[[webflux-http]]
|
|
|
|
= HTTP
|
2018-09-19 10:12:37 +08:00
|
|
|
|
2021-12-14 06:57:36 +08:00
|
|
|
All HTTP-based communication should be protected with xref:features/exploits/http.adoc#http[using TLS].
|
2018-09-19 10:12:37 +08:00
|
|
|
|
2021-04-22 05:01:26 +08:00
|
|
|
This section covers details about using WebFlux-specific features that assist with HTTPS usage.
|
2019-11-26 05:49:51 +08:00
|
|
|
|
|
|
|
[[webflux-http-redirect]]
|
|
|
|
== Redirect to HTTPS
|
|
|
|
|
2021-04-22 05:01:26 +08:00
|
|
|
If a client makes a request using HTTP rather than HTTPS, you can configure Spring Security to redirect to HTTPS.
|
2019-11-26 05:49:51 +08:00
|
|
|
|
2021-04-22 05:01:26 +08:00
|
|
|
The following Java configuration redirects any HTTP requests to HTTPS:
|
2019-11-26 05:49:51 +08:00
|
|
|
|
|
|
|
.Redirect to HTTPS
|
2023-06-19 10:30:41 +08:00
|
|
|
[tabs]
|
|
|
|
======
|
|
|
|
Java::
|
|
|
|
+
|
2020-09-18 20:43:23 +08:00
|
|
|
[source,java,role="primary"]
|
2018-09-19 10:12:37 +08:00
|
|
|
----
|
|
|
|
@Bean
|
2019-07-25 00:15:32 +08:00
|
|
|
SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
|
2018-09-19 10:12:37 +08:00
|
|
|
http
|
|
|
|
// ...
|
2019-07-22 21:31:10 +08:00
|
|
|
.redirectToHttps(withDefaults());
|
2018-09-19 10:12:37 +08:00
|
|
|
return http.build();
|
|
|
|
}
|
|
|
|
----
|
2020-09-18 20:43:23 +08:00
|
|
|
|
2023-06-19 10:30:41 +08:00
|
|
|
Kotlin::
|
|
|
|
+
|
2020-09-18 20:43:23 +08:00
|
|
|
[source,kotlin,role="secondary"]
|
|
|
|
----
|
|
|
|
@Bean
|
|
|
|
fun springSecurityFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
|
|
|
|
return http {
|
|
|
|
// ...
|
|
|
|
redirectToHttps { }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
----
|
2023-06-19 10:30:41 +08:00
|
|
|
======
|
2018-09-19 10:12:37 +08:00
|
|
|
|
2021-04-22 05:01:26 +08:00
|
|
|
You can wrap the configuration can be wrapped around an `if` statement to be turned on only in production.
|
|
|
|
Alternatively, you can enable it by looking for a property about the request that happens only in production.
|
|
|
|
For example, if the production environment adds a header named `X-Forwarded-Proto`, you should use the following Java Configuration:
|
2018-09-19 10:12:37 +08:00
|
|
|
|
2019-11-26 05:49:51 +08:00
|
|
|
.Redirect to HTTPS when X-Forwarded
|
2023-06-19 10:30:41 +08:00
|
|
|
[tabs]
|
|
|
|
======
|
|
|
|
Java::
|
|
|
|
+
|
2020-09-18 20:43:23 +08:00
|
|
|
[source,java,role="primary"]
|
2018-09-19 10:12:37 +08:00
|
|
|
----
|
|
|
|
@Bean
|
2019-07-25 00:15:32 +08:00
|
|
|
SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
|
2018-09-19 10:12:37 +08:00
|
|
|
http
|
|
|
|
// ...
|
2020-01-10 20:10:36 +08:00
|
|
|
.redirectToHttps(redirect -> redirect
|
|
|
|
.httpsRedirectWhen(e -> e.getRequest().getHeaders().containsKey("X-Forwarded-Proto"))
|
2019-07-22 21:31:10 +08:00
|
|
|
);
|
2018-09-19 10:12:37 +08:00
|
|
|
return http.build();
|
|
|
|
}
|
|
|
|
----
|
2020-09-18 20:43:23 +08:00
|
|
|
|
2023-06-19 10:30:41 +08:00
|
|
|
Kotlin::
|
|
|
|
+
|
2020-09-18 20:43:23 +08:00
|
|
|
[source,kotlin,role="secondary"]
|
|
|
|
----
|
|
|
|
@Bean
|
|
|
|
fun springSecurityFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
|
|
|
|
return http {
|
|
|
|
// ...
|
|
|
|
redirectToHttps {
|
|
|
|
httpsRedirectWhen {
|
|
|
|
it.request.headers.containsKey("X-Forwarded-Proto")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
----
|
2023-06-19 10:30:41 +08:00
|
|
|
======
|
2019-11-26 05:49:51 +08:00
|
|
|
|
|
|
|
[[webflux-hsts]]
|
|
|
|
== Strict Transport Security
|
|
|
|
|
2021-07-31 05:56:54 +08:00
|
|
|
Spring Security provides support for xref:servlet/exploits/headers.adoc#servlet-headers-hsts[Strict Transport Security] and enables it by default.
|
2019-11-26 05:49:51 +08:00
|
|
|
|
|
|
|
[[webflux-http-proxy-server]]
|
|
|
|
== Proxy Server Configuration
|
|
|
|
|
2021-08-11 04:21:42 +08:00
|
|
|
Spring Security xref:features/exploits/http.adoc#http-proxy-server[integrates with proxy servers].
|