Get overrideScheme from isSecure(). (#2003)
This commit is contained in:
committed by
GitHub
parent
5ef66ac8f1
commit
652a44bc4d
@@ -94,17 +94,19 @@ public class ReactiveLoadBalancerClientFilter implements GlobalFilter, Ordered {
|
||||
"Unable to find instance for " + url.getHost());
|
||||
}
|
||||
|
||||
ServiceInstance retrievedInstance = response.getServer();
|
||||
|
||||
URI uri = exchange.getRequest().getURI();
|
||||
|
||||
// if the `lb:<scheme>` mechanism was used, use `<scheme>` as the default,
|
||||
// if the loadbalancer doesn't provide one.
|
||||
String overrideScheme = null;
|
||||
String overrideScheme = retrievedInstance.isSecure() ? "https" : "http";
|
||||
if (schemePrefix != null) {
|
||||
overrideScheme = url.getScheme();
|
||||
}
|
||||
|
||||
DelegatingServiceInstance serviceInstance = new DelegatingServiceInstance(
|
||||
response.getServer(), overrideScheme);
|
||||
retrievedInstance, overrideScheme);
|
||||
|
||||
URI requestUrl = reconstructURI(serviceInstance, uri);
|
||||
|
||||
|
||||
@@ -35,6 +35,7 @@ import org.springframework.cloud.gateway.support.NotFoundException;
|
||||
import org.springframework.cloud.loadbalancer.core.ReactorServiceInstanceLoadBalancer;
|
||||
import org.springframework.cloud.loadbalancer.core.RoundRobinLoadBalancer;
|
||||
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
|
||||
import org.springframework.cloud.loadbalancer.support.ServiceInstanceListSuppliers;
|
||||
import org.springframework.cloud.loadbalancer.support.ServiceInstanceSuppliers;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
@@ -262,6 +263,31 @@ public class ReactiveLoadBalancerClientFilterTests {
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void shouldOverrideSchemeUsingIsSecure() {
|
||||
URI url = UriComponentsBuilder.fromUriString("lb://myservice").build().toUri();
|
||||
ServerWebExchange exchange = MockServerWebExchange
|
||||
.from(MockServerHttpRequest.get("https://localhost:9999/mypath").build());
|
||||
exchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, url);
|
||||
ServiceInstance serviceInstance = new DefaultServiceInstance("myservice1",
|
||||
"myservice", "localhost", 8080, false);
|
||||
when(clientFactory.getInstance("myservice",
|
||||
ReactorServiceInstanceLoadBalancer.class)).thenReturn(
|
||||
new RoundRobinLoadBalancer(ServiceInstanceListSuppliers
|
||||
.toProvider("myservice", serviceInstance), "myservice", -1));
|
||||
when(chain.filter(exchange)).thenReturn(Mono.empty());
|
||||
|
||||
filter.filter(exchange, chain).block();
|
||||
|
||||
assertThat((LinkedHashSet<URI>) exchange
|
||||
.getAttribute(GATEWAY_ORIGINAL_REQUEST_URL_ATTR)).contains(url);
|
||||
assertThat((URI) exchange.getAttribute(GATEWAY_REQUEST_URL_ATTR))
|
||||
.isEqualTo(URI.create("http://localhost:8080/mypath"));
|
||||
verify(chain).filter(exchange);
|
||||
verifyNoMoreInteractions(chain);
|
||||
}
|
||||
|
||||
private ServerWebExchange testFilter(MockServerHttpRequest request, URI uri) {
|
||||
return testFilter(MockServerWebExchange.from(request), uri);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user