DEV Community

loading...
Cover image for How to setup a gateway for Swagger UI

How to setup a gateway for Swagger UI

Maxime Guilbert
Just a dev who touches a lot of things and wants to share his experiences. (or just want to note somewhere things to don't forget)
・1 min read

If you want to setup a Swagger UI pod in your Kubernetes cluster and you have a gateway, you will have some issue if you just have exposed the main route.

If you have the issue, you will have the next message which appears in a modal with an input:

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


Solution

To use it correctly, you need to allow a list of paths :

  • /swagger-ui/**
  • /swagger-resources/**
  • /swagger-ui.html
  • /v2/api-docs
  • /webjars/**
  • /view/** - [This one is configurable]

Examples

Spring Security Correction Example

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);
}
Enter fullscreen mode Exit fullscreen mode

source

Spring Cloud Gateway predicates

spring:
  cloud:
    gateway:
      routes:
        - id: swagger-ui
          uri: ${swagger-ui_url}
          predicates:
            - Path=/swagger-ui/**,/swagger-resources/**,/swagger-ui.html,/v2/api-docs,/webjars/**,/view/**
Enter fullscreen mode Exit fullscreen mode

I hope it will help you!

Discussion (0)