Springdoc OpenAPI UI 1.5.7中Petstore默认URL禁用失效问题求助
I've run into this exact issue with springdoc-openapi-ui 1.5.7 before — the disable-swagger-default-url: true config only affects the redirect behavior from your custom path, but doesn't block direct access to /swagger-ui/index.html which still loads the Petstore page. Let's break down why this happens and how to fix it:
Why Your Current Config Isn't Working
In version 1.5.7, the disable-swagger-default-url property is designed to prevent your custom /swagger-ui.html path from redirecting to the default Petstore UI. However, it does not intercept direct requests to the static /swagger-ui/index.html resource served by the library. That's why you can still access the Petstore page by hitting that URL directly.
Solutions to Block the Default Petstore Page
Option 1: Use a Spring MVC Interceptor to Redirect/Block Direct Access
Create a custom interceptor to catch requests to /swagger-ui/index.html and either redirect them to your custom UI path or return a 403 Forbidden response:
import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @Configuration public class SwaggerUiSecurityInterceptor implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new HandlerInterceptorAdapter() { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException { String requestUri = request.getRequestURI(); if ("/swagger-ui/index.html".equals(requestUri)) { // Redirect to your custom Swagger UI path response.sendRedirect("/swagger-ui.html"); // Alternatively, block access entirely: // response.setStatus(HttpServletResponse.SC_FORBIDDEN); return false; } return true; } }); } }
Option 2: Override SwaggerUiConfigProperties to Force Your API Docs
Manually configure the SwaggerUiConfigProperties bean to set your API docs URL as the default — this way, even if someone accesses /swagger-ui/index.html directly, it will load your API docs instead of Petstore:
import org.springdoc.core.SwaggerUiConfigProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class CustomSwaggerUiConfiguration { @Bean public SwaggerUiConfigProperties swaggerUiConfigProperties() { SwaggerUiConfigProperties props = new SwaggerUiConfigProperties(); props.setPath("/swagger-ui.html"); props.setConfigUrl("/v3/api-docs/swagger-config"); props.setDisableSwaggerDefaultUrl(true); // Set your API docs as the default URL for the UI props.setUrl("/v3/api-docs"); return props; } }
Option 3: Upgrade to a Newer Springdoc Version
This bug was fixed in later versions (starting from 1.6.x). If you can upgrade your dependency, this will be the cleanest solution. Update your build.gradle to use a newer stable version:
implementation('org.springdoc:springdoc-openapi-ui:1.6.15') { exclude group: 'javax.validation', module: 'validation-api' }
With the upgrade, your original application.yaml configuration will work as expected — disable-swagger-default-url: true will block both redirects and direct access to the default Petstore page.
Note on Your Path Change
When you changed path to /swagger_ui.html, that only modified your custom access path — it didn't affect the default /swagger-ui/index.html resource. The reason you thought it "worked" is likely because you were testing your custom path, not the default one. The core issue of direct access to Petstore remained.
内容的提问来源于stack exchange,提问作者Tasari

