10.17 生成环境禁用swagger常用方式

0x01:使用@ConditionalOnProperty注解

使用注解

@ConditionalOnProperty(name = "swagger2.enable", havingValue = "true") 

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

@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", "[email protected]"))
.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

swagger2.enable: true


0x02:使用注解@Profile

@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", "[email protected]"))
.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方法

@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", "[email protected]"))
.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

swagger2.enable: true


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


分享到:


相關文章: