springdoc-openapi icon indicating copy to clipboard operation
springdoc-openapi copied to clipboard

Generated server url is HTTP

Open derlin opened this issue 6 years ago • 6 comments

Describe the bug When deployed, the server URL is right, except for the scheme: it is using HTTP instead of HTTPS, hence resulting in "Fail to fetch" errors.

To Reproduce

  • deploy the app on localhsot
  • use nginx to make it available under https
  • open swagger ui

Expected behavior

  • the generated server URL should match the root of the documentation

Screenshots

As we can see, the root URL is https://bbdata.daplab.ch, but the generated server URL is http://....

image

Additional context I am using version 1.4.1.

Here is the relevant part of my application.properties:

## Documentation
# Paths: UI at /docs/api, yaml at /docs/api/bbdata{.yaml}
springdoc.swagger-ui.path=/docs/api
springdoc.api-docs.path=/docs/api/bbdata
# do not expand tags
springdoc.swagger-ui.doc-expansion=none
# sort tags alphabetically
springdoc.swagger-ui.tags-sorter=alpha
springdoc.swagger-ui.operations-sorter=alpha
# show actuator endpoints
springdoc.show-actuator=true

derlin avatar Jun 11 '20 08:06 derlin

I was able to make it work be specifying the server url as / in the configuration:

@OpenAPIDefinition(
    servers = arrayOf(Server(url = "/"))
)
@SpringBootApplication
class App{
// ...
}

derlin avatar Jun 11 '20 11:06 derlin

I was able to make it work be specifying the server url as / in the configuration:

@OpenAPIDefinition(
    servers = arrayOf(Server(url = "/"))
)
@SpringBootApplication
class App{
// ...
}

You'd just saved my whole career!

chientrm avatar Nov 27 '20 08:11 chientrm

2 more possible solutions (duplicated issues):

  • https://github.com/springdoc/springdoc-openapi/issues/118#issuecomment-585777113
  • https://github.com/springdoc/springdoc-openapi/issues/171#issuecomment-556573535

sobelmanus avatar Feb 09 '21 06:02 sobelmanus

Below worked for me.

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

1130657 avatar Jun 29 '21 20:06 1130657

I was able to make it work be specifying the server url as / in the configuration:

@OpenAPIDefinition(
    servers = arrayOf(Server(url = "/"))
)
@SpringBootApplication
class App{
// ...
}

But this is changing my generated server url to / , I am also facing the same issue of https not present in the url

kaus02 avatar Jul 16 '21 04:07 kaus02

For those who know that all proxied requests should always be considered secure, and want to have the full URL, you can set up an override at Spring's filter level:

@Component
public class SecureForwardedHeaderFilter extends ForwardedHeaderFilter {
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        super.doFilterInternal(request, response, new SecureRequestWrappedFilterChain(filterChain));
    }

    static class SecureRequestWrappedFilterChain implements FilterChain {
        private final FilterChain delegate;

        public SecureRequestWrappedFilterChain(FilterChain delegate) {
            this.delegate = delegate;
        }

        @Override
        public void doFilter(ServletRequest request, ServletResponse response) throws ServletException, IOException {
            delegate.doFilter(new SecureRequest((HttpServletRequest) request), response);
        }
    }

    private static class SecureRequest extends HttpServletRequestWrapper {
        public SecureRequest(HttpServletRequest request) {
            super(request);
        }

        @Override
        public boolean isSecure() {
            return true;
        }

        @Override
        public String getScheme() {
            return "https";
        }

        @Override
        public StringBuffer getRequestURL() {
            return new StringBuffer(super.getRequestURL().toString().replaceAll("^http://", "https://"));
        }
    }
}

(then use server.forward-headers-strategy: none in the application config to avoid conflicts)

Obviously the better fix is to have the proxy to set the X-Forwarded-Proto header correctly in the first place, but this is a decent workaround for people who are not in a position to change the proxy config.

(also to link: the duplicate issue seems to be #171)

davidje13 avatar Feb 09 '22 10:02 davidje13