Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Springfox swagger-ui.html unable to infer base URL - Caused by missing cookies

We have our Spring Boot services behind an API Gateway. With an earlier version of Springfox - 2.1.2 we had no issues in loading the swagger-ui.html page. This worked with Spring Boot 1.4.3.RELEASE. From then, we have upgraded to Boot 1.5.7 and upgraded Springfox to 2.8.0.

Now if we load the page we get an alert box with the following long message.

Unable to infer base url. This is common when using dynamic servlet registration or when the API is behind an API Gateway. The base url is the root of where all the swagger resources are served. For e.g. if the api is available at http://example.org/api/v2/api-docs then the base url is http://example.org/api/. Please enter the location manually

I got some hints searching online, but it does not seem those situations apply to us. For one, if I simply revert back the versions, it starts working again through the same API Gateway.

Tracking the traffic, it seems calls to three XHR resources made by the .html page is causing issues. These are returning 401 from our API gateway. And the reason they return 401 is because the cookies are not passed along.

The three calls are:

  • https://base_address/base_context/swagger-resources/configuration/ui
  • https://base_address/base_context/swagger-resources/configuration/security
  • https://base_address/base_context/swagger-resources

If I load these URLs as pure browser requests - they work - because cookies are sent.

I doubt if CORS applies since the HTML is being served from the same address as the swagger JSON and actual service calls.

Any idea why this may be happening? Anybody faced similar issues? Suggestions for workaround? Thanks much in advance.

like image 835
Arnab Gupta Avatar asked Mar 07 '18 15:03

Arnab Gupta


People also ask

How do I get swagger UI in HTML?

Generating HTML documentation using Swagger-ui.Add Swagger-ui dependency - (https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui). This will provide an endpoint at the root url, which provides the Swagger HTML documentation. This will be located at localhost:8080/swagger-ui.

What is @enableswagger2?

Swagger2 is an open source project used to generate the REST API documents for RESTful web services. It provides a user interface to access our RESTful web services via the web browser. To enable the Swagger2 in Spring Boot application, you need to add the following dependencies in our build configurations file.


1 Answers

Add in the security config -- following URLS that are skipped for authentication ::

private static final String[] AUTH_WHITELIST = {         "/swagger-resources/**",         "/swagger-ui.html",         "/v2/api-docs",         "/webjars/**" };  @Override public void configure(WebSecurity web) throws Exception {     web.ignoring().antMatchers(AUTH_WHITELIST); } 
like image 185
panksy2k Avatar answered Sep 19 '22 03:09

panksy2k