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

Swagger ui stuck on unable to infer base url #1996

Closed
logicaleak opened this Issue Aug 23, 2017 · 38 comments

Comments

Projects
None yet
@logicaleak
Copy link

logicaleak commented Aug 23, 2017

Hello,

I am trying to run swagger-ui via springfox. My service is behind an edge service, and from swagger I am constantly getting the error :
screen shot 2017-08-23 at 16 01 41

My docket config is as follows

return new Docket(DocumentationType.SWAGGER_2)
                .host("http://localhost:8999")
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();

I have tried different possibilities of host configuration, including not providing it at all.

I can access the docs json from http://localhost:8999/v2/api-docs just fine.
When I try to set the base url through this message, no matter what I enter, the dialog flickers and just stands there. I check the network and the browser makes a request to /configuration, which seems to return 200.

I have no idea what to do at this point. Does anyone have any idea ?

@dilipkrish

This comment has been minimized.

Copy link
Member

dilipkrish commented Aug 31, 2017

Couple of things to try...

  • Can you brining it up when you run it locally?
  • If you can, then its most likely your gateway is not setting the X-FORWARD... headers correctly
  • If you can't then you have some issues in your swagger-configuration.

In any case, you need to override the host name with caution. Its really only to facilitate tweaking if everything else doesnt work.

@logicaleak

This comment has been minimized.

Copy link
Author

logicaleak commented Sep 3, 2017

I cannot bring it up in local because even on local I run the environment with docker compose, hence the edge service is still in front of the main service.

@DexterYang1982

This comment has been minimized.

Copy link

DexterYang1982 commented Oct 2, 2017

i faced the same problem
make sure your swagger configuration is under your spring boot application which can be scanned.
and then it solved.

@danieldietrich

This comment has been minimized.

Copy link

danieldietrich commented Oct 9, 2017

I had the same problem using Spring Boot 2.0.0.M4 + Spring Security. This solved my issue:

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
class SecurityConfig extends WebSecurityConfigurerAdapter {

    private static final String[] AUTH_WHITELIST = {

            // -- swagger ui
            "/swagger-resources/**",
            "/swagger-ui.html",
            "/v2/api-docs",
            "/webjars/**"
    };
    
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers(AUTH_WHITELIST).permitAll()
                .antMatchers("/**/*").denyAll();
    }
}

I don't know, if the whitelist is complete or correct at all. But it solves the issue described above.

@logicaleak

This comment has been minimized.

Copy link
Author

logicaleak commented Oct 9, 2017

This is somewhat irrelevant now, but what you say has nothing to do with the issue

@danieldietrich

This comment has been minimized.

Copy link

danieldietrich commented Oct 9, 2017

@logicaleak Ok, thank you - it seems to be the same symptom for a different reason in my case. However, maybe it is useful for someone (like me) googling that error message.

Here is what I get if I remove "/swagger-resource/**" from the AUTH_WHITELIST described above:

screen shot 2017-10-09 at 15 32 50

@skozlovich

This comment has been minimized.

Copy link

skozlovich commented Oct 19, 2017

@dilipkrish what should the X-FORWARD headers contain? I can bring it up locally just fine, but when deploying behind a load balancer in AWS I get that dreaded "Unable to infer base url" popup

@dilipkrish

This comment has been minimized.

Copy link
Member

dilipkrish commented Oct 26, 2017

@skozlovich Your X-FORWARD headers would contain your upstream url.

For e.g. if you're doing request path based routing in your api gateway

https://host/path -> https://upstream:port/

Then your X-Forward headers should reflect the upstream information

@igor-holly

This comment has been minimized.

Copy link

igor-holly commented Nov 2, 2017

Try to clear the browser cache.

@pedorro

This comment has been minimized.

Copy link

pedorro commented Dec 18, 2017

I just like to chime in here that I'm seeing this exact same issue. Since the upgrade to Springfox v2.7.0 from v2.4.0 (which fixed #799 - thanks! 😃 ), I'm presented with the same response as in the screenshot above when attempting to view the Swagger UI page. I've tried refreshing cache, switching browsers, and adjusting permissions, even so far as just setting 'permitAll()' to all urls. No luck. I'll keep poking at it and post back here if I come up with anything.

FWIW - I've also tried upgrading to the latest Spring Boot (v1.5.9), Spring Security (v4.2.3), and Guava (v23.5-jre). Still getting the same issue

@dilipkrish

This comment has been minimized.

Copy link
Member

dilipkrish commented Dec 19, 2017

@pedorro You need to find out what path returns a valid json for the path http(s)://host:port/<your-mystery-path>/swagger-resources/configuration/security. Once you know what you-mystery-path is then your base path url is http(s)://host:port/<your-mystery-path>

@pedorro

This comment has been minimized.

Copy link

pedorro commented Dec 19, 2017

@dilipkrish thanks for the quick response. I've got a base url path (I think?) by defining the property springfox.documentation.swagger.v2.path. That seems to set the path for what would otherwise be /v2/api-docs, and also the base path for swagger-ui.html. That does not seem to change the base path for /swagger-resources/configuration/security though. That is still mapping to the root url, as is everything at the /swagger-resources path.

I think I've gotten everything working by adding a number of explicit redirects for urls that it's unable to find because they are not mapped to that base path. The explicit redirects I've added are:

  • <base-path>/swagger-resources -> /swagger-resources
  • <base-path>/swagger-resources/configuration/security -> /swagger-resources/configuration/security
  • <base-path>/swagger-resources/configuration/ui -> /swagger-resources/configuration/ui
  • <base-path>/<base-path> -> <base-path> (this is a weird one)

I discovered this list of required redirects by watching the requests made by swagger-ui.html and seeing which were coming back as 404s. One particularly weird one is that it's looking for the /v2/api-docs json at <base-path>/<base-path>, instead of just <base-path>.

Anyway, I think I have it working now.

Thanks for your help! And for supporting this library! It really is invaluable.

@dilipkrish dilipkrish added this to the 2.8.0 milestone Jan 14, 2018

@dilipkrish dilipkrish closed this Jan 14, 2018

@wafflebot wafflebot bot removed the current label Jan 14, 2018

@palossyl

This comment has been minimized.

Copy link

palossyl commented Mar 23, 2018

Do you have the

@EnableSwagger2

annotation on your SpringApplication?
If missing, causes popping up the above message box!

@jagatrsingh

This comment has been minimized.

Copy link

jagatrsingh commented Apr 9, 2018

The message is promoted in a catch block which does not determine or throw actual http error code which is confusing for many. In short any problem in invoking and the url or getting the response output will throw this error which could be due to URL context/base path issue, permission issue, spring web container not returning json values etc. ( In my case web container was returning xml and also initially i did not have protected urls )

Best solution from framework point of view will be to catch different exceptions and give appropriate message to users.

@codernonkey

This comment has been minimized.

Copy link

codernonkey commented May 22, 2018

I was running into the same issue. Once I allowed all access to /swagger-resources/** I could access the API as normal.
Looking with the dev-console of the browser, I realized that the swagger is NOT sending the Session/Credentials Information to the server, which causes the Server to return a 401. As swagger then misses the required information, it shows the "unable to infer baseurl" screen. However, as the correct base url again gives a 401, you end up in an endless loop.

Interestingly, for all other resources, the browser is sending the session/credential information. So I assume the bug is not on server side but swagger fails to send the (XMLHttp)Request to swagger-resources without the withCredentials flag set (https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/withCredentials).

So for me the fix was to not require authentication/authorization for /swagger-resources/** resources.

@dilipkrish

This comment has been minimized.

Copy link
Member

dilipkrish commented May 31, 2018

@codernonkey thanks for your suggestion. I believe it was fixed in 2.9.0, so I just wanted to check if that was the version you're using.

@jagatrsingh thanks for your input, in this case, since its the browser, you should be able to inspect the console to find out what happened.

@jonathannaguin

This comment has been minimized.

Copy link

jonathannaguin commented Jun 15, 2018

@dilipkrish I just upgraded to 2.9.0 and I am still getting this problem. Does Swagger work at all with Spring Security?

@dilipkrish

This comment has been minimized.

Copy link
Member

dilipkrish commented Jun 17, 2018

You mean swagger-ui? Yes it does, but you need to configure it correctly for it to work. The simplest option is to permit anonymous access to

  • /swagger-ui.html
  • /swagger-resources/**
  • /v2/api-docs
  • /webjars/**
    as shown here.

If thats not an option you'd need to protect some of those resources via basic authentication, typically an authentication mechanism (may be different from the one protecting the APIs themselves).

@codernonkey

This comment has been minimized.

Copy link

codernonkey commented Jun 20, 2018

@dilipkrish I was and am using version 2.8.0 from https://mvnrepository.com/artifact/io.springfox but am not sure if this is the right source as there is no 2.9.0 available?

@dilipkrish

This comment has been minimized.

Copy link
Member

dilipkrish commented Jun 21, 2018

@codernonkey its available in jcenter.

@shuchitaprasad

This comment has been minimized.

Copy link

shuchitaprasad commented Jun 25, 2018

The solution suggested by @danieldietrich worked for me :-)

@ropherpanama

This comment has been minimized.

Copy link

ropherpanama commented Jul 4, 2018

Same problem here.
Swagger 2.9.2, adding my SpringFoxConfigclass to getRootConfigClasses solve the problem.

public class ApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer  {
@Override
protected Class<?>[] getRootConfigClasses() {
	return new Class[] { ApplicationConfiguration.class, SpringFoxConfig.class };
}

@Override
protected Class<?>[] getServletConfigClasses() {
	return null;
}

@Override
protected String[] getServletMappings() {
	return new String[] { "/" };
}
}
@dilipkrish

This comment has been minimized.

Copy link
Member

dilipkrish commented Jul 5, 2018

@ropherpanama thanks for sharing your tip

@timvl

This comment has been minimized.

Copy link

timvl commented Jul 10, 2018

Adding @EnableSwagger2 did the trick for me

@DranonJoD

This comment has been minimized.

Copy link

DranonJoD commented Sep 6, 2018

Facing the same issue and the line, in my case, that cause the problem is
mapper.enableDefaultTyping(DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);

in the definition of the objectMapper bean :

       @Bean
	public ObjectMapper objectMapper() {
		ObjectMapper mapper = new ObjectMapper();
		mapper.enableDefaultTyping(DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
		mapper.setSerializationInclusion(Include.NON_NULL);
		mapper.registerModule(new JavaTimeModule());
		mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
		return mapper;
	}

Sadly, I need that line... it makes the json of th api starts with ["springfox.documentation.spring.web.json.Json",{"swagger":"2.0","
the ui do not like that and I've not find a way to get the ui to work.

Forgot to add that I have a MappingJackson2HttpMessageConverter that uses the object mapper.

@ArunPrasathSubramani

This comment has been minimized.

Copy link

ArunPrasathSubramani commented Sep 19, 2018

I have faced similar issue.
My environment is Weblogic 12.2.1, Spring 4.3.3, Swagger 2.8.0, Guava 24.0-jre.
I was trying all the whitelisting of swagger resources, etc.

Finally, got to know I overlooked the logs missing jackson jars version mismatch, as my server's jackson took precedence.

fixed it with

       <wls:prefer-application-packages>
            <wls:package-name>com.fasterxml.jackson</wls:package-name>
        </wls:prefer-application-packages>

Now, Swagger is up and running.

@oeresundsgruppen

This comment has been minimized.

Copy link

oeresundsgruppen commented Nov 5, 2018

Works if I run with java -jar abc.war, but not if I deploy the abc.war to tomcat. The popup continues and cancel does not work. Cancel even gives get 404 on path/null/swagger-resources. The null is not included if I click OK instead. This could be fixed in your next release maybe.

@oeresundsgruppen

This comment has been minimized.

Copy link

oeresundsgruppen commented Nov 5, 2018

It even at times looks for path/swagger-ui.html/swagger-resources/configuration/ui - something is wrong in the javascript... to be fixed maybe ?

@oeresundsgruppen

This comment has been minimized.

Copy link

oeresundsgruppen commented Nov 5, 2018

The web is floating with issues in respect to swagger springfox - time to get this fixed maybe

@dilipkrish

This comment has been minimized.

Copy link
Member

dilipkrish commented Nov 7, 2018

@oeresundsgruppen looking to fix this, would appreciate a sample project that you replicate it not working.

@waqas64

This comment has been minimized.

Copy link

waqas64 commented Nov 8, 2018

I am facing the same issue on localhost. I am trying to connect swagger-ui to an existing swagger.json (not auto-generated). Followed this project here but with version 2.9.2
''https://github.com/indrabasak/springfox-ui-from-json-example''
I can see the swagger JSON on /v2/api-docs but calling http://localhost:8080/swagger-ui.html#/ gives me the "unable to infer base url" error.
Tried most of the proposed solutions, no success yet!

@oeresundsgruppen

This comment has been minimized.

Copy link

oeresundsgruppen commented Nov 9, 2018

@dilipkrish

This comment has been minimized.

Copy link
Member

dilipkrish commented Nov 12, 2018

@oeresundsgruppen Thats an old repo that I submitted a PR for to fix your setup. Nothing has changed since. Do you have some commits you haven't pushed perhaps?

@oeresundsgruppen

This comment has been minimized.

Copy link

oeresundsgruppen commented Nov 12, 2018

The example works under spring-run/java -jar, but not under plain simple tomcat - and hence demonstrates the problems to be resolved. This is unfortunately still open

@xwhfcenter

This comment has been minimized.

Copy link

xwhfcenter commented Dec 6, 2018

Do you have the

@EnableSwagger2

annotation on your SpringApplication?
If missing, causes popping up the above message box!

I add this annotation, and fix the problem

@oeresundsgruppen

This comment has been minimized.

Copy link

oeresundsgruppen commented Dec 6, 2018

Indeed I did, are you deploying under tomcat ?

@zhangzhenhua92

This comment has been minimized.

Copy link

zhangzhenhua92 commented Dec 28, 2018

my project is not for springboot . but for spring-mvc 's swagger config class like this:
`@EnableSwagger2
@EnableWebMvc
public class SwaggerConfig {
@bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any()) //显示所有类
//.apis(RequestHandlerSelectors.withClassAnnotation(Api.class)) //只显示添加@Api注解的类
.build()
.apiInfo(apiInfo());
}

private ApiInfo apiInfo() {
    return new ApiInfoBuilder()
            .title("**订单api")  //粗标题
            .description("controller层接口描述")   //描述
            .version("1.2.3")   //api version
            .termsOfServiceUrl("http://abc.com")
            .license("http://abc.com")   //链接名称
            .licenseUrl("http://abc.com")   //链接地址
            .build();
}

}
`
i can't get solution to solve my problem. who can help me?
Addition: i can run it locally.

@pravgcet

This comment has been minimized.

Copy link

pravgcet commented Jan 9, 2019

I got it working by adding @Configuration along with @EnableSwagger2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment