swagger-ui
production
disable
configuration
API security

How to disable Swagger UI in production

Codemia Knowledge Hub ·

Swagger UI is useful during development, but leaving it enabled in production can reveal your API's endpoints and methods to unauthorized users. The usual fix is to load the Swagger configuration only in non-production environments.

What Swagger UI exposes

Swagger UI is part of the Swagger framework, an open-source project used to design, build, document and consume RESTful web services. It gives a browser interface for calling the API's endpoints and seeing their responses. In production that same interface shows anyone the structure of your API.

Why turn it off in production

  • Security: a live Swagger UI can reveal sensitive endpoints and methods.
  • Performance: serving Swagger UI consumes resources, even if only a little.
  • Smaller attack surface: deployment best practice is to expose as little as possible.

Disabling Swagger UI in Spring Boot

How you disable it depends on your stack. In a Spring Boot application, Swagger is commonly configured with the springfox-swagger-ui dependency, and you can keep it out of production by controlling its configuration conditionally:

  1. Profile-based configuration: use Spring profiles so the Swagger configuration loads only in specific environments (for example dev or test). Annotate the configuration class with @Profile, and in any other profile, including prod, the Docket bean and @EnableSwagger2 are never registered:
java
1import org.springframework.context.annotation.Bean;
2import org.springframework.context.annotation.Configuration;
3import org.springframework.context.annotation.Profile;
4import springfox.documentation.builders.PathSelectors;
5import springfox.documentation.builders.RequestHandlerSelectors;
6import springfox.documentation.spi.DocumentationType;
7import springfox.documentation.spring.web.plugins.Docket;
8import springfox.documentation.swagger2.annotations.EnableSwagger2;
9
10@Configuration
11@EnableSwagger2
12@Profile({"dev", "test"})
13public class SwaggerConfig {
14
15    @Bean
16    public Docket api() {
17        return new Docket(DocumentationType.SWAGGER_2)
18            .select()
19            .apis(RequestHandlerSelectors.any())
20            .paths(PathSelectors.any())
21            .build();
22    }
23}

Start production with --spring.profiles.active=prod and Swagger UI is not served.

  1. Property switch with springdoc-openapi: if the project uses springdoc-openapi instead of springfox, turn the UI and the generated docs off in the production properties file (for example application-prod.properties):
properties
springdoc.swagger-ui.enabled=false
springdoc.api-docs.enabled=false

If Swagger UI must stay reachable

  • Version control: keep Swagger UI in sync with API changes to avoid inconsistencies in development.
  • Restricted access: if you absolutely need it in production, put it behind restricted access or API keys.
  • Monitoring: if Swagger UI is enabled under controlled circumstances, set up monitoring to alert on unauthorized use attempts.

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design