Swagger 3.0.0:没有 SwaggerConfig 和 @Profile 就无法在生产中禁用

我正在从 2.x 升级到 SpringFox Swagger 3.0.0,它引入了 Spring Boot 启动器 springfox-boot-starter 依赖项,无需基于 2.x 的 SwaggerConfig:

I'm upgrading to SpringFox Swagger 3.0.0 from 2.x, which introduces the Spring Boot starter springfox-boot-starter dependency that obviates the need for the 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();
    }
}

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

Now that I no longer need this @Configuration, which allows me to specify my environment profiles in @Profile and therefore disable Swagger in production, how do I disable Swagger in production in SpringFox Swagger-UI 3.x?

注意:这里讨论了基于 Spring Security 的方法对某些人来说是一种选择,但在这种情况下不是一种选择,原因有两个:

NOTE: There is Spring Security-based approached discussed here that could be an option for some, but is not an option for this scenario for two reasons:

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

推荐答案

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

The answer was not easy to find and was NOT in SpringFox's migration guide or documentation here (where it should be).

Swagger UI 3.0.0 的正确且迄今为止最好的答案是此处.

The CORRECT and by far best answer for Swagger UI 3.0.0 is here.

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

Just add springfox.documentation.enabled=[true|false] to the target environment's application.properties or application.yml.

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

As an aside, it would be nice to see a section with the list of all available Spring Boot properties listed in the SpringFox doc.

相关文章