Swagger 3.0.0:不能在没有 SwaggerConfig 和 @Profile 的情况下在生产中禁用 [英] Swagger 3.0.0: Can't disable in production without SwaggerConfig and @Profile

查看:221
本文介绍了Swagger 3.0.0:不能在没有 SwaggerConfig 和 @Profile 的情况下在生产中禁用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从 2.x 升级到 SpringFox Swagger 3.0.0,它引入了 Spring Boot starter 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.

这篇关于Swagger 3.0.0:不能在没有 SwaggerConfig 和 @Profile 的情况下在生产中禁用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆