Spring webflux:将http重定向到https [英] Spring webflux: redirect http to https
本文介绍了Spring webflux:将http重定向到https的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在spring webflux中配置http-> https重定向?我需要将所有 http
请求重定向到 https
(因为我理解任何 http
请求应具有 301
http状态响应,并更改http-> https)。在中未找到任何相关信息文档。
我发现这个回答,但它与tomcat有关。我有网络。
How can i configure http->https redirect in spring webflux? I need all http
request be redirected to https
(as i understood any http
request should have 301
http status response with change http->https). Didn't found any information about it in documentation.
I found this answer, but it related to tomcat. I have netty.
推荐答案
我找到了方法,希望它有所帮助:
I found way, hope it helps somebody:
@Bean
public WebFilter httpsRedirectFilter() {
return new WebFilter() {
@Override
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
URI originalUri = exchange.getRequest().getURI();
//here set your condition to http->https redirect
List<String> forwardedValues = exchange.getRequest().getHeaders().get("x-forwarded-proto");
if (forwardedValues != null && forwardedValues.contains("http")) {
try {
URI mutatedUri = new URI("https",
originalUri.getUserInfo(),
originalUri.getHost(),
originalUri.getPort(),
originalUri.getPath(),
originalUri.getQuery(),
originalUri.getFragment());
ServerHttpResponse response = exchange.getResponse();
response.setStatusCode(HttpStatus.MOVED_PERMANENTLY);
response.getHeaders().setLocation(mutatedUri);
return Mono.empty();
} catch (URISyntaxException e) {
throw new IllegalStateException(e.getMessage(), e);
}
}
return chain.filter(exchange);
}
};
}
这篇关于Spring webflux:将http重定向到https的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文