From 9cb9319fed3eaddcdaedd134f77328bd031f0a94 Mon Sep 17 00:00:00 2001 From: James Barnett Date: Tue, 21 Nov 2023 17:45:23 +0000 Subject: [PATCH] Check for x-forwarded-proto when constructing API base url --- .../uk/gov/beis/els/api/openapi/OpenApiService.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/main/java/uk/gov/beis/els/api/openapi/OpenApiService.java b/src/main/java/uk/gov/beis/els/api/openapi/OpenApiService.java index 337bfccb..47236216 100644 --- a/src/main/java/uk/gov/beis/els/api/openapi/OpenApiService.java +++ b/src/main/java/uk/gov/beis/els/api/openapi/OpenApiService.java @@ -23,6 +23,7 @@ import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; import org.springframework.web.servlet.support.ServletUriComponentsBuilder; +import org.springframework.web.util.UriComponentsBuilder; import uk.gov.beis.els.api.common.ApiDocumentationController; import uk.gov.beis.els.api.common.TagNotFoundException; import uk.gov.beis.els.api.model.OperationWithSchema; @@ -66,9 +67,15 @@ public String getApiSpecUrl(HttpServletRequest request) { } public String getBaseUrl(HttpServletRequest request) { - return ServletUriComponentsBuilder.fromRequestUri(request) - .replacePath(null) - .build() + UriComponentsBuilder builder = ServletUriComponentsBuilder.fromRequestUri(request) + .replacePath(null); + + String xForwardedProto = request.getHeader("X-Forwarded-Proto"); + if (xForwardedProto != null) { + builder.scheme(xForwardedProto); + } + + return builder.build() .toUriString(); }