[英]Spring Cloud Gateway: how to redirect to other host
I have a request/route that should be redirected to another server.我有一个应该重定向到另一台服务器的请求/路由。 For this, I thought using a redirect filter from 'spring-cloud-gateway' should be the right thing.
为此,我认为使用来自“spring-cloud-gateway”的重定向过滤器应该是正确的。 Example: http://localhost:8080/site/rest/services/testservice/1 should be redirected to https://internal-api.com/site/rest/services/testservice/1 .
示例: http://localhost:8080/site/rest/services/testservice/1 应该重定向到https://internal-api.com/site/rest/services/testservice/1 。
So far I came up with the following filters in a RouteLocator
:到目前为止,我在
RouteLocator
中提出了以下过滤器:
@Bean
public RouteLocator routes(RouteLocatorBuilder builder) {
String auth = username + ":" + password;
String encodedAuth = Base64.getEncoder().encodeToString(auth.getBytes());
return builder.routes()
.route("geoApi", r -> r
.path("/site/rest/services/**")
.filters(f -> {
f.setRequestHeader(HttpHeaders.AUTHORIZATION, "Basic " + encodedAuth);
f.redirect(302, "https://internal-api.com");
return f;
})
.uri("https://internal-api.com"))
.build();
}
The redirect works, but only goes to the "root" url of the internal API and not the "/site/rest/...".重定向有效,但仅转到内部 API 的“根”url,而不是“/site/rest/...”。 How can I fix this?
我怎样才能解决这个问题?
This is how I achieved to correctly redirect it.这就是我正确重定向它的方法。 I'm not sure if this is the cleanest solution though.
我不确定这是否是最干净的解决方案。 I couldn't get spencergibb's input to work...
我无法让 spencergibb 的输入工作...
f.changeRequestUri(e -> {
UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromUri(e.getRequest().getURI());
String modifiedUri = uriBuilder.scheme("https").host("internal-api.com").port(null).toUriString();
return Optional.of(URI.create(modifiedUri));
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.