SpringBoot 在生产快速禁用Swagger2

SpringBoot 在生产快速禁用Swagger2

yizhiwazi

0.6 2018.07.21 19:31* 字数 219 阅读 4858评论 11喜欢 52

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

学习目标

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

快速查阅

源码下载:SpringBoot Swagger2 Auto Close

专题阅读:《SpringBoot 布道系列》

使用教程

禁用方法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();
    }
}

8069210-1857e405f827555a.png
image

访问效果:

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

8069210-c73fce8dc62e52dd.png
image

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

8069210-a17d421b8f8c2808.png
image
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值