Eugene Khyst
Eugene Khyst

Reputation: 10315

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.

Upvotes: 25

Views: 48698

Answers (6)

Alisha Raju
Alisha Raju

Reputation: 840

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")));
    }
}

Generated server url is HTTP - issue

Upvotes: -1

Raju Reddy
Raju Reddy

Reputation: 11

Make it default to "/" using @Server annotation. This will make the swagger APIs use https

import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import io.swagger.v3.oas.annotations.servers.Server;

@OpenAPIDefinition(servers = {@Server(url = "/", description = "Default Server URL")})
public class FormService implements ApplicationRunner {
....
}

Upvotes: 1

alaster
alaster

Reputation: 4181

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))
                // ...
    }
}

Upvotes: 9

user6070687
user6070687

Reputation: 381

I had same problem. Below worked for me.

@OpenAPIDefinition( 
    servers = {
       @Server(url = "/", description = "Default Server URL")
    }
) 
@SpringBootApplication
public class App {
    // ...
}

Upvotes: 38

Peter Lustig
Peter Lustig

Reputation: 1701

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.

Upvotes: 13

Eugene Khyst
Eugene Khyst

Reputation: 10315

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:

Upvotes: 9

Related Questions