Skip to content

Unable to override the inferred server url when using springfox with a proxy #3531

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
rowleyn opened this issue Sep 3, 2020 · 8 comments
Closed

Comments

@rowleyn
Copy link

rowleyn commented Sep 3, 2020

What version of the library are you using? Is it the latest version? The latest released version is Download
Latest, 3.0.0

What kind of issue is this?

Question. Is this a question about how to do a certain thing?
I'm using springfox with an api that is behind a proxy. The proxy requires that the api have its address set to the local loopback address (127.0.0.1). Because of this, springfox is inferring the server url as https://127.0.0.1:443, instead of the actual url of the api. This causes requests sent from the swagger ui to fail, because they are being sent to local loopback instead of to the actual public api address. How can I override the inferred url to point the swagger ui at the correct address?

@dilipkrish
Copy link
Member

Couple of things here

  • Are you using the x-forward headers as recommended by spring in your proxy?
  • Have you see the other issues where you have a work around available?

@dilipkrish
Copy link
Member

Also this is a duplicate of #3445

@dilipkrish dilipkrish added this to the 3.0.1 milestone Sep 4, 2020
@rowleyn
Copy link
Author

rowleyn commented Sep 4, 2020

We are using the x-forward headers in the proxy.

Our issue is not a base path issue, but a case of being unable to set the server url. We tried to override it using the servers method on the Docket class, but it seems that is bugged as per #3483. Our paths after the server url are just fine.

@dilipkrish
Copy link
Member

Yeah, until the fix is out this is a work around

@rowleyn
Copy link
Author

rowleyn commented Sep 8, 2020

The workaround worked, except I needed to use WebMvcOpenApiTransformationFilter instead because we are upgraded to Open Api. Also, I'm using it to set the server of the swagger docs, not the base path.

In Java:

@Component
public class SpringfoxSwaggerHostResolver implements WebMvcOpenApiTransformationFilter {

  @Value("${swagger.host}") String hostUri;

  @Override
  public OpenAPI transform(OpenApiTransformationContext<HttpServletRequest> context) {
    OpenAPI swagger = context.getSpecification();
    Server server = new Server();
    server.setUrl(hostUri);
    swagger.setServers(Arrays.asList(server));
    return swagger;
  }

  @Override
  public boolean supports(DocumentationType docType) {
    return docType.equals(DocumentationType.OAS_30);
  }
}

@zhugw
Copy link

zhugw commented Nov 13, 2020

I think just add this config could work

 proxy_set_header Host $host;

and you also could dynamically substitute servers url of /v3/api-docs e.g

location /v3/api-docs {
    proxy_pass http://XXX:8081/v3/api-docs;
    sub_filter 'http://XXX' 'https://YYY';
    sub_filter_types application/json;
}

@stale
Copy link

stale bot commented Feb 11, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Feb 11, 2021
@stale
Copy link

stale bot commented Feb 25, 2021

This issue has been automatically closed because it has not had recent activity. Please re-open a new issue if this is still an issue.

@stale stale bot closed this as completed Feb 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants