Sub topic 14
Protecting or disabling the docs in production
This chapter assumes you have a Spring Boot 3 application with springdoc-openapi added, and that you know basic Spring Security configuration and Spring profiles. By default, the OpenAPI JSON and Swagger UI are served to anyone who can reach the application. That is convenient in development. In production it hands attackers a complete map of your endpoints, parameters and models.
Why exposure matters
The OpenAPI document lists every path, HTTP method, parameter and schema that springdoc finds. It does not expose data or secrets. It does remove the guesswork from probing your API. You have three options:
- Leave the docs open. This suits internal tools or public APIs that are meant to be documented.
- Require authentication for the docs endpoints.
- Turn the docs off in production.
Letting the docs through Spring Security
When Spring Security protects the application, it also blocks the documentation endpoints, so Swagger UI returns 401 or redirects to a login page. The filter chain below opens the docs paths and requires authentication for everything else.
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.SecurityFilterChain;
@Configuration
public class SecurityConfig {
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.authorizeHttpRequests(auth -> auth
.requestMatchers("/v3/api-docs/**", "/swagger-ui/**", "/swagger-ui.html").permitAll()
.anyRequest().authenticated());
return http.build();
}
}This is the development setup. To protect the docs instead, remove the permitAll() line, or replace it with a rule such as .hasRole("ADMIN"). The docs then need the same credentials as the API.
Two details cause common errors:
- If you changed
springdoc.api-docs.pathorspringdoc.swagger-ui.path, update the matchers to the new paths. - If you use grouped APIs,
/v3/api-docs/**already covers the per-group documents.
Disabling the docs with a profile
The safest option is not to serve the docs at all. Put these properties in a production profile file so they apply only when the prod profile is active.
# application-prod.yml
springdoc:
api-docs:
enabled: false
swagger-ui:
enabled: falseWith api-docs.enabled set to false, /v3/api-docs returns 404. Swagger UI depends on that document, so the UI stops working too. Setting swagger-ui.enabled to false also removes the UI page itself, so the endpoint is gone and not just broken.
Activate the profile when you start the application:
java -jar app.jar --spring.profiles.active=prodYou can check the result with curl -i http://localhost:8080/v3/api-docs. You should see a 404 status. Check /swagger-ui.html the same way.
Common mistakes
| Mistake | Effect | Fix |
|---|---|---|
| Setting the properties in `application.yml` instead of `application-prod.yml` | Docs are off in development too | Keep the disabling properties in the profile file |
| Forgetting to activate the `prod` profile | Docs stay public | Set `spring.profiles.active` in the deployment |
| Disabling only the UI | The JSON at `/v3/api-docs` is still served | Disable `api-docs` as well |
| Permitting docs paths in the security chain but disabling the docs | No harm, but the rule is dead code | Remove the rule or make it profile-specific |
Exercise
Your production deployment must not reveal the API description to anyone, but developers still need Swagger UI locally. Which file holds the springdoc properties, and which two properties do you set?
Show answer
Put them in application-prod.yml, so they apply only under the prod profile. Set springdoc.api-docs.enabled: false and springdoc.swagger-ui.enabled: false. Then run production with spring.profiles.active=prod.
Exercise
After you add Spring Security, Swagger UI at /swagger-ui.html redirects to a login page. What change opens the docs paths, and why is that change a poor fit for production?
Show answer
Add a requestMatchers("/v3/api-docs/**", "/swagger-ui/**", "/swagger-ui.html").permitAll() rule before anyRequest().authenticated(). In production it makes the full API description readable by anyone, so remove the rule, restrict it to a role, or disable the docs with a profile.