• 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;
        @Bean
        public 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();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

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

    @Configuration
    @EnableSwagger2
    @Profile({"local", "dev"})
    public class Swagger2Config {
        @Bean
        public 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();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    方法三:使用注解@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 {
        @Bean
        public 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();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

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

    #Swagger lock
    swagger:    
        enabled: true
    
    • 1
    • 2
    • 3

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

  • 相关阅读:
    ArcGIS:如何迭代Shp文件所有要素并分别导出为Shp文件?
    板凳--------第60章 SOCKET:服务器设计
    【计算机网络】互连网的路由选择协议概述
    Elastic Stack 和 Docker Compose 入门:第 2 部分
    vue配置多个服务端请求地址并实现scp2自动部署
    day4:Node.js 核心库
    JAVA计算机毕业设计医生在线诊所平台Mybatis+系统+数据库+调试部署
    订阅发布和请求响应都是消息传递模式
    Redis内存回收
    Java 开源开发平台 O2OA V7.2.0 发布,新增系统配置图形化模块和企业文件模块!
  • 原文地址:https://blog.csdn.net/m0_46459413/article/details/130709016