Swagger 3.0.0:不能在没有 SwaggerConfig 和 @Profile 的情况下在生产中禁用

sam*_*usa 2 java swagger swagger-ui spring-boot springfox-boot-starter

我正在从 2.x 升级到 SpringFox Swagger 3.0.0,它引入了 Spring Boot starterspringfox-boot-starter依赖项,消除了对 2.x-based 的需求SwaggerConfig

/**
 * NO LONGER NEEDED
 */
@Configuration
@EnableSwagger2
@Profile({"local", "dev", "beta"}) // <- HOW TO DISABLE IN PROD INSTEAD OF THIS
public class SwaggerConfig {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我不再需要这个@Configuration,它允许我指定我的环境配置文件@Profile并因此在生产中禁用 Swagger,我如何在 SpringFox Swagger-UI 3.x 中禁用生产中的 Swagger?

注意:这里讨论了基于 Spring Security 的方法这可能是某些人的选择,但由于两个原因,这不是这种情况的选择:

  • 我的应用程序不使用 Spring Security,并且不可能包含spring-boot-security-starter依赖项
  • 它需要将所有其他端点列入白名单才能让它们再次工作,这是不可接受的

sam*_*usa 6

答案并不容易找到,并且不在 SpringFox 的迁移指南或此处的文档中(应该在哪里)。

Swagger UI 3.0.0 的正确且迄今为止最好的答案在这里

只需添加springfox.documentation.enabled=[true|false]到目标环境的 application.properties 或 application.yml 即可。

顺便说一句,很高兴看到包含 SpringFox 文档中列出的所有可用 Spring Boot 属性列表的部分。