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:
- Profile-based configuration: use Spring profiles so the Swagger configuration loads only in specific environments (for example
devortest). Annotate the configuration class with@Profile, and in any other profile, includingprod, theDocketbean and@EnableSwagger2are never registered:
Start production with --spring.profiles.active=prod and Swagger UI is not served.
- Property switch with springdoc-openapi: if the project uses
springdoc-openapiinstead of springfox, turn the UI and the generated docs off in the production properties file (for exampleapplication-prod.properties):
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.
Related reading
- How do you use NextToken in AWS API calls
- How does a new node join a group in the SWIM protocol?
- How does AMQP overcome the difficulties of using TCP directly?
- How does asychronous programming work in Netty? Does it make things more chatty?
- How do you use bcrypt for hashing passwords in PHP?
- How does Kafka specify key alias for Client Authentication?
- How does erlang handle distributed networks?
- How does google prediction API work
System design interview problems on this topic
- Design a Load Balancer: solution and walkthrough
- Design a Domain Name System: solution and walkthrough
- Design a Global Content Distribution Network: solution and walkthrough
- Design an API Rate Limiter: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.