DEV Community

Cover image for How to setup a gateway for Swagger UI
Maxime Guilbert
Maxime Guilbert

Posted on • Edited on

How to setup a gateway for Swagger UI

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!

Top comments (0)