'Wrong "Generated server url" in springdoc-openapi-ui (Swagger UI) deployed behind proxy
Spring Boot 2.2 application with springdoc-openapi-ui (Swagger UI) runs HTTP port. The application is deployed to Kubernetes with Ingress routing HTTPS requests from outside the cluster to the service.
In this case Swagger UI available at https://example.com/api/swagger-ui.html has wrong "Generated server url" - http://example.com/api. While it should be https://example.com/api.
While Swagger UI is accessed by HTTPS, the generated server URL still uses HTTP.
Solution 1:[1]
springdoc-openapi FAQ has a section How can I deploy the Doploy springdoc-openapi-ui, behind a reverse proxy?.
The FAQ section can be extended.
Make sure X-Forwarded headers are sent by your proxy (X-Forwarded-For, X-Forwarded-Proto and others).
If you are using Undertow (spring-boot-starter-undertow), set property server.forward-headers-strategy=NATIVE to make a Web server natively handle X-Forwarded headers. Also, consider switching to Undertow if you are not using it.
If you are using Tomcat (spring-boot-starter-tomcat), set property server.forward-headers-strategy=NATIVE and make sure to list IP addresses of all internal proxies to trust in the property server.tomcat.internal-proxies=192\\.168\\.\\d{1,3}\\.\\d{1,3}. By default, IP addresses in 10/8, 192.168/16, 169.254/16 and 127/8 are trusted.
Alternatively, for Tomcat set property server.forward-headers-strategy=FRAMEWORK.
Useful links:
Solution 2:[2]
If the accepted solution doesn't work for you then you can always set the url manually by defining a bean.
@Bean
public OpenAPI customOpenAPI() {
Server server = new Server();
server.setUrl("https://example.com/api");
return new OpenAPI().servers(List.of(server));
}
And the url can be defined via a property and injected here.
Solution 3:[3]
I had same problem. Below worked for me.
@OpenAPIDefinition(
servers = {
@Server(url = "/", description = "Default Server URL")
}
)
@SpringBootApplication
public class App {
// ...
}
Solution 4:[4]
Below worked for me.
@OpenAPIDefinition(servers = {@server(url = "/", description = "Default Server URL")})
@SpringBootApplication
class App{
// ...
}
or
@OpenAPIDefinition(servers = {@server(url = "/", description = "Default Server URL")})
@Configuration
public class OpenAPIConfig {
@Bean
public OpenAPI customOpenAPI() {
return new OpenAPI()
.info(new Info().title("App name")
.termsOfService("http://swagger.io/terms/")
.license(new License().name("Apache 2.0").url("http://springdoc.org")));
}
}
Solution 5:[5]
In case you have non-default context path
@Configuration
public class SwaggerConfig {
@Bean
public OpenAPI openAPI(ServletContext servletContext) {
Server server = new Server().url(servletContext.getContextPath());
return new OpenAPI()
.servers(List.of(server))
// ...
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Peter Lustig |
| Solution 3 | user6070687 |
| Solution 4 | Alisha Raju |
| Solution 5 | alaster |
