Spring4.2+Spring Security4.2+Swagger2.8.0:无法推断base url如何解决?
Hey there, I’ve fought this exact issue with Springfox 2.8.0 before—those generic answers floating around rarely hit the mark for this specific version. Let’s dive into targeted, tested fixes that should get your Swagger UI working again:
1. Explicitly Set the Context Path in Swagger Configuration
Springfox 2.8.0 has a known bug with automatic context path inference, especially if your app runs with a custom context path. Even if you don’t use a context path, explicitly defining it can resolve the inference failure.
Update your Swagger config class:
@Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.basePackage("com.your.app.package")) .paths(PathSelectors.any()) .build() // Replace with your app's context path, or "/" if none .pathMapping("/your-context-path"); } }
2. Manually Map Swagger Static Resources
Springfox 2.8.0 often fails to auto-configure the static resources needed for Swagger UI. Adding explicit resource mappings fixes this:
Create or update your WebMvc config class:
@Configuration public class WebMvcConfig implements WebMvcConfigurer { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { // Map Swagger UI HTML registry.addResourceHandler("swagger-ui.html") .addResourceLocations("classpath:/META-INF/resources/"); // Map WebJars (Swagger UI dependencies) registry.addResourceHandler("/webjars/**") .addResourceLocations("classpath:/META-INF/resources/webjars/"); } }
3. Exclude Swagger Paths from Interceptors/Filters
If you have custom interceptors, filters, or Spring Security configured, they might be blocking Swagger’s internal requests. Add exclusions for these critical paths:
For Custom Interceptors:
@Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(yourCustomInterceptor) .excludePathPatterns( "/swagger-resources/**", "/swagger-ui.html", "/v2/api-docs", "/webjars/**" ); }
For Spring Security:
@Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers( "/swagger-resources/**", "/swagger-ui.html", "/v2/api-docs", "/webjars/**" ).permitAll() .anyRequest().authenticated(); } }
4. Manually Specify the API Host (For Proxy/Non-Standard Deployments)
If your API is behind a reverse proxy or deployed on a non-standard host/port, Springfox can’t infer the base URL correctly. Add the host() method to your Docket configuration:
@Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) // ... other config .host("your-api-host.com:8080"); // Replace with your actual API host:port }
Start with fixes 1 and 2 first—they resolve the majority of "Unable to infer base url" cases in Springfox 2.8.0. If those don’t work, move on to checking interceptors/security rules or setting the host explicitly.
内容的提问来源于stack exchange,提问作者user9464567

