Browse Source

Get overrideScheme from isSecure(). (#2003)

pull/2019/head
Olga Maciaszek-Sharma 4 years ago committed by GitHub
parent
commit
652a44bc4d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/ReactiveLoadBalancerClientFilter.java
  2. 26
      spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/filter/ReactiveLoadBalancerClientFilterTests.java

6
spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/ReactiveLoadBalancerClientFilter.java

@ -94,17 +94,19 @@ public class ReactiveLoadBalancerClientFilter implements GlobalFilter, Ordered { @@ -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);

26
spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/filter/ReactiveLoadBalancerClientFilterTests.java

@ -35,6 +35,7 @@ import org.springframework.cloud.gateway.support.NotFoundException; @@ -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 { @@ -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);
}

Loading…
Cancel
Save