mirror of
https://github.com/spring-projects/spring-security.git
synced 2025-05-31 09:12:14 +00:00
Add WebFlux Redirect to HTTPS Reference
Fixes: gh-5869
This commit is contained in:
parent
db9248e05a
commit
501c008526
@ -27,6 +27,7 @@ import java.util.Collections;
|
|||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
import org.springframework.security.core.context.ReactiveSecurityContextHolder;
|
import org.springframework.security.core.context.ReactiveSecurityContextHolder;
|
||||||
import org.springframework.security.oauth2.client.oidc.userinfo.OidcUserRequest;
|
import org.springframework.security.oauth2.client.oidc.userinfo.OidcUserRequest;
|
||||||
@ -1473,6 +1474,22 @@ public class ServerHttpSecurity {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Configures when this filter should redirect to https
|
||||||
|
*
|
||||||
|
* By default, the filter will redirect whenever an exchange's scheme is not https
|
||||||
|
*
|
||||||
|
* @param when determines when to redirect to https
|
||||||
|
* @return the {@link HttpsRedirectSpec} for additional configuration
|
||||||
|
*/
|
||||||
|
public HttpsRedirectSpec httpsRedirectWhen(
|
||||||
|
Function<ServerWebExchange, Boolean> when) {
|
||||||
|
ServerWebExchangeMatcher matcher = e -> when.apply(e) ?
|
||||||
|
ServerWebExchangeMatcher.MatchResult.match() :
|
||||||
|
ServerWebExchangeMatcher.MatchResult.notMatch();
|
||||||
|
return httpsRedirectWhen(matcher);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Configures a custom HTTPS port to redirect to
|
* Configures a custom HTTPS port to redirect to
|
||||||
*
|
*
|
||||||
|
@ -31,7 +31,7 @@ Below are the highlights of the release.
|
|||||||
** <<webflux-headers-csp,Content Security Policy>>
|
** <<webflux-headers-csp,Content Security Policy>>
|
||||||
** <<webflux-headers-feature,Feature Policy>>
|
** <<webflux-headers-feature,Feature Policy>>
|
||||||
** <<webflux-headers-referrer,Referrer Policy>>
|
** <<webflux-headers-referrer,Referrer Policy>>
|
||||||
* Support for redirecting to HTTPS
|
* <<webflux-redirect-https,Redirect to HTTPS>>
|
||||||
|
|
||||||
=== Integrations
|
=== Integrations
|
||||||
|
|
||||||
|
@ -4,6 +4,8 @@ include::webflux.adoc[leveloffset=+1]
|
|||||||
|
|
||||||
include::headers.adoc[leveloffset=+1]
|
include::headers.adoc[leveloffset=+1]
|
||||||
|
|
||||||
|
include::redirect-https.adoc[leveloffset=+1]
|
||||||
|
|
||||||
include::oauth2/index.adoc[leveloffset=+1]
|
include::oauth2/index.adoc[leveloffset=+1]
|
||||||
|
|
||||||
include::registered-oauth2-authorized-client.adoc[leveloffset=+1]
|
include::registered-oauth2-authorized-client.adoc[leveloffset=+1]
|
||||||
|
@ -0,0 +1,32 @@
|
|||||||
|
[[webflux-redirect-https]]
|
||||||
|
= Redirect to HTTPS
|
||||||
|
|
||||||
|
HTTPS is required to provide a secure application.
|
||||||
|
Spring Security can be configured to perform a redirect to https using the following Java Configuration:
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
----
|
||||||
|
@Bean
|
||||||
|
SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
|
||||||
|
http
|
||||||
|
// ...
|
||||||
|
.redirectToHttps();
|
||||||
|
return http.build();
|
||||||
|
}
|
||||||
|
----
|
||||||
|
|
||||||
|
The configuration can easily be wrapped around an if statement to only be turned on in production.
|
||||||
|
Alternatively, it can be enabled by looking for a property about the request that only happens in production.
|
||||||
|
For example, if the production environment adds a header named `X-Forwarded-Proto` the following Java Configuration could be used:
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
----
|
||||||
|
@Bean
|
||||||
|
SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
|
||||||
|
http
|
||||||
|
// ...
|
||||||
|
.redirectToHttps()
|
||||||
|
.httpsRedirectWhen(e -> e.getRequest().getHeaders().containsKey("X-Forwarded-Proto"));
|
||||||
|
return http.build();
|
||||||
|
}
|
||||||
|
----
|
Loading…
x
Reference in New Issue
Block a user