SpringBoot quickly disable the production Swagger2

SpringBoot quickly disable the production Swagger2

yizhiwazi

0.6 2018.07.21 19:31 * 219 words read like 52 4858 11 comments

Are you still open Swagger production node it, and quickly to stop this behavior interfaces expose it.

learning target

Quickly learn to use annotations closed Swagger2, to avoid repeated exposure interfaces.

Quick View

Download Source: SpringBoot Swagger2 Auto use Close

Thematic read: "SpringBoot sermon series"

Tutorial

Prohibited Method 1: Use annotations @Profile({"dev","test"})indicate on the development or test environment, and the production closed. (Recommended Use)

Prohibited Method 2: Use annotations @ConditionalOnProperty(name = "swagger.enable", havingValue = "true")and add swagger.enable test configuration or in development configuration = true to open, fill in the production environment is not turned off by default Swagger.

E.g:

/**
 * 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

Access Effect:

Development Environment: HTTP: // localhost: 8081 / Swagger-ui.html normal access Swagger.

8069210-c73fce8dc62e52dd.png
image

Production environment: HTTP: // localhost: 8082 / Swagger-ui.html has been disabled Swagger.

8069210-a17d421b8f8c2808.png
image

Reproduced in: https: //www.jianshu.com/p/4e474d5361e6

Guess you like

Origin blog.csdn.net/weixin_34000916/article/details/91279445