Skip to content

Commit 02215f1

Browse files
committed
Spring Boot (Webflux) - Swagger UI - redirect URI does not include Gateway Prefix. Fixes #2708
1 parent 832669b commit 02215f1

File tree

3 files changed

+61
-8
lines changed

3 files changed

+61
-8
lines changed

springdoc-openapi-starter-webflux-ui/src/test/java/test/org/springdoc/ui/app32/SpringDocBehindProxyBasePathTest.java springdoc-openapi-starter-webflux-ui/src/test/java/test/org/springdoc/ui/app33/SpringDocBehindProxyBasePathTest.java

+10-8
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*
1717
*/
1818

19-
package test.org.springdoc.ui.app32;
19+
package test.org.springdoc.ui.app33;
2020

2121
import jakarta.annotation.PostConstruct;
2222
import org.junit.jupiter.api.Test;
@@ -37,8 +37,8 @@
3737

3838

3939
@SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT,
40-
properties = { //"spring.webflux.base-path=/test",
41-
"server.forward-headers-strategy=native",
40+
properties = { "spring.webflux.base-path=/test",
41+
"server.forward-headers-strategy=framework",
4242
"server.port=9318",
4343
"springdoc.swagger-ui.path=/documentation/swagger-ui.html",
4444
"springdoc.api-docs.path=/documentation/v3/api-docs",
@@ -47,7 +47,8 @@
4747
@Import(SpringDocConfig.class)
4848
public class SpringDocBehindProxyBasePathTest extends AbstractCommonTest {
4949

50-
private static final String X_FORWARD_PREFIX = "/path/prefix";
50+
public static final String WEBFLUX_BASE_PATH = "/test";
51+
public static final String X_FORWARD_PREFIX = "/path/prefix";
5152

5253
@LocalServerPort
5354
private int port;
@@ -62,21 +63,22 @@ void init() {
6263

6364
@Test
6465
public void testIndex() throws Exception {
65-
HttpStatusCode httpStatusMono = webClient.get().uri("/documentation/swagger-ui.html")
66+
HttpStatusCode httpStatusMono = webClient.get().uri(WEBFLUX_BASE_PATH+"/documentation/swagger-ui.html")
6667
.header("X-Forwarded-Prefix", X_FORWARD_PREFIX)
6768
.exchangeToMono(clientResponse -> Mono.just(clientResponse.statusCode())).block();
6869
assertThat(httpStatusMono).isEqualTo(HttpStatus.FOUND);
6970

70-
httpStatusMono = webClient.get().uri("/documentation/webjars-pref/swagger-ui/index.html")
71+
httpStatusMono = webClient.get().uri(WEBFLUX_BASE_PATH+"/documentation/webjars-pref/swagger-ui/index.html")
7172
.header("X-Forwarded-Prefix", X_FORWARD_PREFIX)
7273
.exchangeToMono(clientResponse -> Mono.just(clientResponse.statusCode())).block();
7374
assertThat(httpStatusMono).isEqualTo(HttpStatus.OK);
7475

75-
String contentAsString = webClient.get().uri("/documentation/v3/api-docs/swagger-config")
76+
String contentAsString = webClient.get().uri(WEBFLUX_BASE_PATH+"/documentation/v3/api-docs/swagger-config")
7677
.header("X-Forwarded-Prefix", X_FORWARD_PREFIX)
7778
.retrieve()
7879
.bodyToMono(String.class).block();
79-
String expected = getContent("results/app32-1.json");
80+
81+
String expected = getContent("results/app33.json");
8082
assertEquals(expected, contentAsString, true);
8183
}
8284

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package test.org.springdoc.ui.app33;
2+
3+
import java.util.List;
4+
5+
import org.apache.commons.lang3.reflect.FieldUtils;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
9+
import org.springframework.boot.autoconfigure.web.reactive.WebFluxProperties;
10+
import org.springframework.context.annotation.Bean;
11+
import org.springframework.context.annotation.Configuration;
12+
import org.springframework.http.server.reactive.ServerHttpRequest;
13+
import org.springframework.util.CollectionUtils;
14+
import org.springframework.web.server.ServerWebExchange;
15+
import org.springframework.web.server.WebFilter;
16+
import org.springframework.web.server.WebFilterChain;
17+
18+
@Configuration
19+
public class SpringDocConfig {
20+
21+
private static final Logger LOGGER = LoggerFactory.getLogger(SpringDocConfig.class);
22+
23+
24+
@Bean
25+
public WebFilter rewritePathWebFilter(WebFluxProperties webFluxProperties) {
26+
return (ServerWebExchange exchange, WebFilterChain chain) -> {
27+
try {
28+
ServerHttpRequest originalRequest = (ServerHttpRequest) FieldUtils.readDeclaredField(exchange.getRequest(), "originalRequest", true);
29+
List<String> forwardedPrefixHeaders = originalRequest.getHeaders().get("X-Forwarded-Prefix");
30+
if (!CollectionUtils.isEmpty(forwardedPrefixHeaders)) {
31+
String forwardedPrefix = forwardedPrefixHeaders.get(0);
32+
ServerHttpRequest mutatedRequest = exchange.getRequest().mutate()
33+
.contextPath(forwardedPrefix + webFluxProperties.getBasePath())
34+
.build();
35+
ServerWebExchange mutatedExchange = exchange.mutate().request(mutatedRequest).build();
36+
return chain.filter(mutatedExchange);
37+
}
38+
39+
} catch (Exception e) {
40+
LOGGER.error(e.getMessage());
41+
}
42+
return chain.filter(exchange);
43+
};
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"configUrl": "/path/prefix/test/documentation/v3/api-docs/swagger-config",
3+
"oauth2RedirectUrl": "http://localhost:9318/path/prefix/test/documentation/webjars-pref/swagger-ui/oauth2-redirect.html",
4+
"url": "/path/prefix/test/documentation/v3/api-docs",
5+
"validatorUrl": ""
6+
}

0 commit comments

Comments
 (0)