前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >生成环境禁用swagger常用方式

生成环境禁用swagger常用方式

作者头像
BUG弄潮儿
发布2020-06-12 16:44:02
1.5K0
发布2020-06-12 16:44:02
举报
文章被收录于专栏:JAVA乐园JAVA乐园

阅读文本大概需要3分钟。

0x01:使用@ConditionalOnProperty注解

使用注解

代码语言:javascript
复制
@ConditionalOnProperty(name = "swagger2.enable", havingValue = "true")  

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

代码语言:javascript
复制
@Configuration
@EnableSwagger2
@ConditionalOnProperty(name = "swagger2.enable", havingValue = "true")
public class Swagger2 extends WebMvcConfigurationSupport {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //为当前包路径
                .apis(RequestHandlerSelectors.basePackage("com.yq.demo.controller"))
                .paths(PathSelectors.any())
                .build();
    }
    //构建 api文档的详细信息函数,注意这里的注解引用的是哪个
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Spring Boot 测试使用 Swagger2 构建RESTful API")
                .contact(new Contact("java乐园", "https://xxx.com", "test@163.com"))
                .version("1.0")
                .description("User API 描述")
                .build();
    }

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/swagger-ui.html").addResourceLocations(
                "classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations(
                "classpath:/META-INF/resources/webjars/");
    }
}

在application.yml配置文件添加,启用swagger

代码语言:javascript
复制
swagger2.enable: true

0x02:使用注解@Profile

代码语言:javascript
复制
@Configuration
@EnableSwagger2
@Profile("dev")
public class Swagger2 extends WebMvcConfigurationSupport {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //为当前包路径
                .apis(RequestHandlerSelectors.basePackage("com.yq.demo.controller"))
                .paths(PathSelectors.any())
                .build();
    }
    //构建 api文档的详细信息函数,注意这里的注解引用的是哪个
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Spring Boot 测试使用 Swagger2 构建RESTful API")
                .contact(new Contact("java乐园", "https://xxx.com", "test@163.com"))
                .version("1.0")
                .description("User API 描述")
                .build();
    }

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/swagger-ui.html").addResourceLocations(
                "classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations(
                "classpath:/META-INF/resources/webjars/");
    }
}

0x03:使用Docket的enable方法

代码语言:javascript
复制
@Configuration
@EnableSwagger2
public class Swagger2 extends WebMvcConfigurationSupport {

    @Value("{swagger2.enable:false}")
    private boolean enableSwagger;

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(enableSwagger)
                .select()
                //为当前包路径
                .apis(RequestHandlerSelectors.basePackage("com.yq.demo.controller"))
                .paths(PathSelectors.any())
                .build();
    }
    //构建 api文档的详细信息函数,注意这里的注解引用的是哪个
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Spring Boot 测试使用 Swagger2 构建RESTful API")
                .contact(new Contact("java乐园", "https://xxx.com", "test@163.com"))
                .version("1.0")
                .description("User API 描述")
                .build();
    }

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/swagger-ui.html").addResourceLocations(
                "classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations(
                "classpath:/META-INF/resources/webjars/");
    }
}

在application.yml配置文件添加,启用swagger

代码语言:javascript
复制
swagger2.enable: true

生成环境禁用Swagger非常必要,防止服务器的全部接口暴露给互联网。因为如果全部的接口暴露给互联网,就存在非常严重的安全隐患。容易受到黑客的攻击。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-10-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 BUG弄潮儿 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
Serverless HTTP 服务
Serverless HTTP 服务基于腾讯云 API 网关 和 Web Cloud Function(以下简称“Web Function”)建站云函数(云函数的一种类型)的产品能力,可以支持各种类型的 HTTP 服务开发,实现了 Serverless 与 Web 服务最优雅的结合。用户可以快速构建 Web 原生框架,把本地的 Express、Koa、Nextjs、Nuxtjs 等框架项目快速迁移到云端,同时也支持 Wordpress、Discuz Q 等现有应用模版一键快速创建。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档