软件编程
位置:首页>> 软件编程>> java编程>> SpringBoot在生产快速禁用Swagger2的方法步骤

SpringBoot在生产快速禁用Swagger2的方法步骤

作者:yizhiwazi  发布时间:2022-12-30 00:05:53 

标签:springboot,禁用,swagger

你还在生产节点开放Swagger吗,赶紧停止这种暴露接口的行为吧。

学习目标

快速学会使用注解关闭Swagger2,避免接口重复暴露。

使用教程

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

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

例如:


/**
* Swagger2 接口配置
*/

@Configuration
@EnableSwagger2
//@Profile({"dev","test"})
@ConditionalOnProperty(name = "swagger.enable", havingValue = "true")
public class Swagger2Config {
 /**
  * 添加摘要信息(Docket)
  */
 @Bean
 public Docket controllerApi() {
   return new Docket(DocumentationType.SWAGGER_2)
       .apiInfo(new ApiInfoBuilder()
           .title("标题:某公司_用户信息管理系统_接口文档")
           .description("描述:用于管理集团旗下公司的人员信息,具体包括XXX,XXX模块...")
           .contact(new Contact("Socks", null, null))
           .version("版本号:1.0")
           .build())
       .select()
       .apis(RequestHandlerSelectors.basePackage("com.hehe.controller"))
       .paths(PathSelectors.any())
       .build();
 }
}

SpringBoot在生产快速禁用Swagger2的方法步骤

访问效果:

开发环境:http://localhost:8081/swagger-ui.html 正常访问Swagger。

SpringBoot在生产快速禁用Swagger2的方法步骤

生产环境:http://localhost:8082/swagger-ui.html   已经禁用Swagger。

SpringBoot在生产快速禁用Swagger2的方法步骤

来源:https://www.jianshu.com/p/34c5180a5134

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com