【问题标题】:Swagger 3.0.0: Can't disable in production without SwaggerConfig and @ProfileSwagger 3.0.0:没有 SwaggerConfig 和 @Profile 无法在生产中禁用
【发布时间】:2021-05-07 17:10:23
【问题描述】:

我正在从 2.x 升级到 SpringFox Swagger 3.0.0,它引入了 Spring Boot 启动器 springfox-boot-starter 依赖项,从而消除了对基于 2.x 的 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,我如何在SpringFox Swagger-UI 3.x 中禁用生产中的Swagger?

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

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

【问题讨论】:

  • 您可以尝试 @Profile({"!prod"}) 并在您的应用程序属性中添加 spring.profiles=prod springfox.documentation.enabled=false
  • 感谢在下面发布参考答案后才看到这个。

标签: java spring-boot swagger swagger-ui springfox-boot-starter


【解决方案1】:

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

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

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

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

【讨论】:

  • 你拯救了我的一天!其他问题中的所有其他答案都是旧的:只有这个有效。
  • 它肯定需要在文档中 :(。Tnx 一堆
猜你喜欢
  • 2019-07-07
  • 1970-01-01
  • 2021-08-09
  • 1970-01-01
  • 2018-03-30
  • 2017-05-21
  • 2022-12-24
  • 2018-08-04
  • 1970-01-01
相关资源
最近更新 更多