当前位置: 首页 > news >正文

springBoot如何【禁用Swagger】

需求:

生产环境下,需要关闭swagger配置,避免接口暴露。

方法:

1、使用注解@Value()
2、使用注解@Profile({“dev”,“test”}) 表示在开发或测试环境开启,而在生产关闭。
3、使用注解@ConditionalOnProperty(name = “swagger.enable”, havingValue = “true”) 然后在测试配置或者开发配置中 添加 swagger.enable = true 即可开启,生产环境不填则默认关闭Swagger。

方法一:使用注解@Value()

在Swagger2Config类里添加;
并需要在配置文件里添加一个swagger.enable属性,根据不同的application-xx.yml进行动态插入true或false即可。

@Configuration
@EnableSwagger2
public class Swagger2Config {@Value("${swagger.enable}")private Boolean enable;@Beanpublic Docket swaggerPersonApi10() {return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.basePackage("com.unidata.cloud.logservice.api.controller")).paths(PathSelectors.any()).enable(enable)    //配置在该处生效.build().apiInfo(apiInfo());}private ApiInfo apiInfo() {return new ApiInfoBuilder().version("1.0").title("xx项目:xx平台 Swagger2 文档 API").contact(new Contact("  xx团队", "https://www.xx.com/", "kangjia@xx.com")).description("logservice platform API v1.0").build();}
}

方法二:使用注解@Profile({“dev”,“test”}) 表示在开发或测试环境开启,而在生产关闭。

@Configuration
@EnableSwagger2
@Profile({"local", "dev"})
public class Swagger2Config {@Beanpublic Docket swaggerPersonApi10() {return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.basePackage("com.unidata.cloud.logservice.api.controller")).paths(PathSelectors.any()).build().apiInfo(apiInfo());}private ApiInfo apiInfo() {return new ApiInfoBuilder().version("1.0").title("xx项目:xx平台 Swagger2 文档 API").contact(new Contact("  xx团队", "https://www.xx.com/", "kangjia@xx.com")).description("logservice platform API v1.0").build();}
}

方法三:使用注解@ConditionalOnProperty(name = “swagger.enable”, havingValue = “true”) 然后在测试配置或者开发配置中 添加 swagger.enable = true 即可开启,生产环境不填则默认关闭Swagger。

1、使用注解 @ConditionalOnProperty(name = “swagger.enable”, havingValue = “true”)

@Configuration
@EnableSwagger2
//@ConditionalOnProperty(name ="enabled" ,prefix = "swagger",havingValue = "true",matchIfMissing = true   //matchIfMissing=true :为空则设为true,不合理
@ConditionalOnProperty(name = “swagger.enable”, havingValue =true)
public class Swagger2Config {@Beanpublic Docket swaggerPersonApi10() {return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.basePackage("com.unidata.cloud.logservice.api.controller")).paths(PathSelectors.any()).enable(enable).build().apiInfo(apiInfo());}private ApiInfo apiInfo() {return new ApiInfoBuilder().version("1.0").title("xx项目:xx平台 Swagger2 文档 API").contact(new Contact("  xx团队", "https://www.xxx.com/", "kangjia@xxx.com")).description("logservice platform API v1.0").build();}
}

2、然后在测试配置或者开发配置中 添加 swagger.enable = true 即可开启,生产环境不写该配置则默认关闭Swagger。

#Swagger lock
swagger:    enabled: true

参考链接:https://www.dandelioncloud.cn/article/details/1593427183718813697

http://www.lryc.cn/news/69907.html

相关文章:

  • ​数据库原理及应用上机(实验四 SQL连接查询)
  • linux上使用系统安装和Docker安装mysql的两种方式
  • 解决Mac下载官网JDK速度过慢的问题
  • 笔记本wifi与台式机、内网服务器共网、共享wifi详细教程
  • 纵观人类发展史,我发现了一个秘密!
  • HDFS的数据流
  • [230531] 托福听力真题|TPO67配套词汇|10:23-11:23
  • 每日学术速递5.21
  • 【SpringBoot】SpringBoot 纯后端项目如何自定义异常页面(Whitelabel Error Page)
  • Netty核心技术三--NIO编程
  • 机器人的运动范围:DFS
  • Rshiny编写ui中具有web依赖项的控件{该问题的具体阐述请看引言}
  • 1700页,卷S人的 软件测试《八股文》PDF手册,涨薪跳槽拿高薪就靠它了
  • bundle的常用命令
  • 一、数据字典介绍
  • 常见的SQL优化
  • Sonic新生态Sonic IDE体验
  • [VRTK4.0]安装VRTKv4Tilia软件包导入程序
  • SpringBoot开发实用篇2---与数据层技术有关的替换和整合
  • 科普ChatGPT
  • Spring MVC的核心类和注解
  • Java 创建一个大文件
  • 董小姐大意了
  • Java高并发核心编程—内置锁原理篇
  • opencv文字识别
  • bool、python集合
  • 从零开始学架构——可扩展架构模式
  • Day03 01-MySQL数据完整性详解
  • DJ 5-4 以太网 Ethernet
  • 华为OD机试真题 Java 实现【区块链文件转储系统】【2023Q2 200分】