Skip to content

Commit f0bfec8

Browse files
author
bnasslahsen
committed
Provide oauth2RedirectUrl, if not declared. Fixes #320
1 parent 950669a commit f0bfec8

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

springdoc-openapi-common/src/main/java/org/springdoc/core/Constants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public final class Constants {
2020
public static final String DEFAULT_WEB_JARS_PREFIX_URL = "/webjars";
2121
public static final String WEB_JARS_PREFIX_URL = "${springdoc.webjars.prefix:#{T(org.springdoc.core.Constants).DEFAULT_WEB_JARS_PREFIX_URL}}";
2222
public static final String SWAGGER_UI_URL = "/swagger-ui/index.html";
23+
public static final String SWAGGER_UI_OAUTH_REDIRECT_URL = "/swagger-ui/oauth2-redirect.html";
2324
public static final String APPLICATION_OPENAPI_YAML = "application/vnd.oai.openapi";
2425
public static final String DEFAULT_SWAGGER_UI_PATH = DEFAULT_PATH_SEPARATOR + "swagger-ui.html";
2526
public static final String SWAGGER_UI_PATH = "${springdoc.swagger-ui.path:#{T(org.springdoc.core.Constants).DEFAULT_SWAGGER_UI_PATH}}";

springdoc-openapi-common/src/main/java/org/springdoc/core/SwaggerUiConfigProperties.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.util.stream.Collectors;
1717

1818
import static org.springdoc.core.Constants.SPRINGDOC_SWAGGER_UI_ENABLED;
19+
import static org.springdoc.core.Constants.SWAGGER_UI_OAUTH_REDIRECT_URL;
1920
import static org.springframework.util.AntPathMatcher.DEFAULT_PATH_SEPARATOR;
2021

2122
/**
@@ -109,7 +110,7 @@ public class SwaggerUiConfigProperties {
109110
/**
110111
* OAuth redirect URL.
111112
*/
112-
private String oauth2RedirectUrl;
113+
private String oauth2RedirectUrl=SWAGGER_UI_OAUTH_REDIRECT_URL;
113114
private String url;
114115

115116
public static void addGroup(String group) {

springdoc-openapi-ui/src/main/java/org/springdoc/ui/SwaggerWelcome.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,7 @@ class SwaggerWelcome {
4242
@Operation(hidden = true)
4343
@GetMapping(SWAGGER_UI_PATH)
4444
public String redirectToUi(HttpServletRequest request) {
45-
String uiRootPath = "";
46-
if (swaggerPath.contains("/"))
47-
uiRootPath = swaggerPath.substring(0, swaggerPath.lastIndexOf('/'));
48-
StringBuilder sbUrl = new StringBuilder();
49-
sbUrl.append(REDIRECT_URL_PREFIX);
50-
if (StringUtils.isNotBlank(mvcServletPath))
51-
sbUrl.append(mvcServletPath);
52-
sbUrl.append(uiRootPath);
45+
StringBuilder sbUrl = new StringBuilder(REDIRECT_URL_PREFIX).append(this.calculateUiRootPath());
5346
sbUrl.append(SWAGGER_UI_URL);
5447
buildConfigUrl(request);
5548
UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromUriString(sbUrl.toString());
@@ -87,7 +80,18 @@ private void buildConfigUrl(HttpServletRequest request) {
8780

8881
}
8982
if (!swaggerUiConfig.isValidUrl(swaggerUiConfig.getOauth2RedirectUrl())) {
90-
swaggerUiConfig.setOauth2RedirectUrl(ServletUriComponentsBuilder.fromCurrentContextPath().path(swaggerUiConfig.getOauth2RedirectUrl()).build().toString());
83+
swaggerUiConfig.setOauth2RedirectUrl(ServletUriComponentsBuilder.fromCurrentContextPath().path(this.calculateUiRootPath().append(swaggerUiConfig.getOauth2RedirectUrl()).toString()).build().toString());
9184
}
9285
}
86+
87+
private StringBuilder calculateUiRootPath() {
88+
String uiRootPath = "";
89+
if (swaggerPath.contains("/"))
90+
uiRootPath = swaggerPath.substring(0, swaggerPath.lastIndexOf('/'));
91+
StringBuilder sbUrl = new StringBuilder();
92+
if (StringUtils.isNotBlank(mvcServletPath))
93+
sbUrl.append(mvcServletPath);
94+
sbUrl.append(uiRootPath);
95+
return sbUrl;
96+
}
9397
}

0 commit comments

Comments
 (0)