Skip to content

Commit 9b1148a

Browse files
committed
Respect the springdoc.cache.disabled setting for recalculating the oauth2 redirect url (as is done with the generated server url in api-docs). When springdoc is reachable under multiple host-names (e.g. through proxies & direct), the authentication-redirect otherwise always contains the host that was generated the first time for that specific instance. A unit-test was also added to ensure this behavior.
1 parent 756ec3e commit 9b1148a

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

springdoc-openapi-ui/src/main/java/org/springdoc/webmvc/ui/SwaggerWelcomeCommon.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import static org.springdoc.core.Constants.SWAGGER_UI_URL;
1717

1818
public abstract class SwaggerWelcomeCommon extends AbstractSwaggerWelcome {
19+
private String originalRelativeOauth2RedirectUrl;
20+
1921
/**
2022
* Instantiates a new Abstract swagger welcome.
2123
* @param swaggerUiConfig the swagger ui config
@@ -28,7 +30,7 @@ public SwaggerWelcomeCommon(SwaggerUiConfigProperties swaggerUiConfig, SpringDoc
2830

2931
protected String redirectToUi(HttpServletRequest request) {
3032
buildConfigUrl(request.getContextPath(), ServletUriComponentsBuilder.fromCurrentContextPath());
31-
String sbUrl = swaggerUiConfigParameters.getUiRootPath() + SWAGGER_UI_URL;
33+
String sbUrl = swaggerUiConfigParameters.getUiRootPath() + SWAGGER_UI_URL;
3234
UriComponentsBuilder uriBuilder = getUriComponentsBuilder(sbUrl);
3335

3436
// forward all queryParams from original request
@@ -44,7 +46,11 @@ protected Map<String, Object> openapiJson(HttpServletRequest request) {
4446

4547
@Override
4648
protected void calculateOauth2RedirectUrl(UriComponentsBuilder uriComponentsBuilder) {
47-
if (!swaggerUiConfigParameters.isValidUrl(swaggerUiConfigParameters.getOauth2RedirectUrl()))
49+
if (!swaggerUiConfigParameters.isValidUrl(swaggerUiConfigParameters.getOauth2RedirectUrl())) {
50+
originalRelativeOauth2RedirectUrl = swaggerUiConfigParameters.getOauth2RedirectUrl();
4851
swaggerUiConfigParameters.setOauth2RedirectUrl(uriComponentsBuilder.path(swaggerUiConfigParameters.getUiRootPath()).path(swaggerUiConfigParameters.getOauth2RedirectUrl()).build().toString());
52+
} else if (springDocConfigProperties.isCacheDisabled() && originalRelativeOauth2RedirectUrl != null) {
53+
swaggerUiConfigParameters.setOauth2RedirectUrl(uriComponentsBuilder.path(swaggerUiConfigParameters.getUiRootPath()).path(originalRelativeOauth2RedirectUrl).build().toString());
54+
}
4955
}
5056
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
*
3+
* * Copyright 2019-2020 the original author or authors.
4+
* *
5+
* * Licensed under the Apache License, Version 2.0 (the "License");
6+
* * you may not use this file except in compliance with the License.
7+
* * You may obtain a copy of the License at
8+
* *
9+
* * https://www.apache.org/licenses/LICENSE-2.0
10+
* *
11+
* * Unless required by applicable law or agreed to in writing, software
12+
* * distributed under the License is distributed on an "AS IS" BASIS,
13+
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* * See the License for the specific language governing permissions and
15+
* * limitations under the License.
16+
*
17+
*/
18+
19+
package test.org.springdoc.ui.app5;
20+
21+
import org.junit.jupiter.api.Test;
22+
import org.springframework.boot.autoconfigure.SpringBootApplication;
23+
import org.springframework.test.context.TestPropertySource;
24+
import test.org.springdoc.ui.AbstractSpringDocTest;
25+
26+
import static org.hamcrest.CoreMatchers.equalTo;
27+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
28+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
29+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
30+
31+
@TestPropertySource(properties = {"server.forward-headers-strategy=framework", "springdoc.cache.disabled=true"})
32+
public class SpringDocOauthRedirectUrlRecalculateTest extends AbstractSpringDocTest {
33+
34+
@Test
35+
public void oauth2_redirect_url_recalculation() throws Exception {
36+
mockMvc.perform(get("/v3/api-docs/swagger-config").header("X-Forwarded-Proto", "https").header("X-Forwarded-Host", "host1"))
37+
.andExpect(status().isOk())
38+
.andExpect(jsonPath("oauth2RedirectUrl", equalTo("https://host1/swagger-ui/oauth2-redirect.html")));
39+
40+
mockMvc.perform(get("/v3/api-docs/swagger-config").header("X-Forwarded-Proto", "http").header("X-Forwarded-Host", "host2:8080"))
41+
.andExpect(status().isOk())
42+
.andExpect(jsonPath("oauth2RedirectUrl", equalTo("http://host2:8080/swagger-ui/oauth2-redirect.html")));
43+
}
44+
45+
@SpringBootApplication
46+
static class SpringDocTestApp {
47+
}
48+
49+
}

0 commit comments

Comments
 (0)