Explain three ways for SpringBoot to disable Swagger in detail
- 2021-12-13 08:07:03
- OfStack
Directory Summary Method Disable Method 1: Disable Method 2: Disable Method 3:
Summary
In a production environment, we need to turn off the swagger configuration to avoid this dangerous behavior of exposing the interface.
Method
Disable Method 1:
Using the annotation @ Value (), it is recommended to use
package com.dc.config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* @author sunny chen
* @version V1.0
* @Package com.dc.config
* @date 2018/1/16 17:33
* @Description: Main purpose: Open online interface documents and add related configurations
*/
@Configuration
@EnableSwagger2
public class Swagger2Config extends WebMvcConfigurerAdapter {
@Value("${swagger.enable}")
private Boolean enable;
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.enable(enable)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.dc.controller"))
.paths(PathSelectors.any())
//.paths(PathSelectors.none())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("auth System data interface document ")
.description(" This system is a new architecture Api Explanatory document ")
.termsOfServiceUrl("")
.contact(new Contact(" Chen Yongjia chen867647213@163.com", "", "https://blog.csdn.net/Mrs_chens"))
.version("1.0")
.build();
}
/**
* swagger ui Resource mapping
* @param registry
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
/**
* swagger-ui.html Path mapping, used in browsers /api-docs Visit
* @param registry
*/
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addRedirectViewController("/api-docs","/swagger-ui.html");
}
}
Disable Method 2:
Use the annotation @ Profile ({"dev", "test"}) to indicate that it is on in the development or test environment and off in production. (Recommended)
package com.dc.config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* @author sunny chen
* @version V1.0
* @Package com.dc.config
* @date 2018/1/16 17:33
* @Description: Main purpose: Open online interface documents and add related configurations
*/
@Configuration
@EnableSwagger2
@Profile({ " dev " , " test " })
public class Swagger2Config extends WebMvcConfigurerAdapter {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.dc.controller"))
.paths(PathSelectors.any())
//.paths(PathSelectors.none())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("auth System data interface document ")
.description(" This system is a new architecture Api Explanatory document ")
.termsOfServiceUrl("")
.contact(new Contact(" Chen Yongjia chen867647213@163.com", "", "https://blog.csdn.net/Mrs_chens"))
.version("1.0")
.build();
}
/**
* swagger ui Resource mapping
* @param registry
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
/**
* swagger-ui.html Path mapping, used in browsers /api-docs Visit
* @param registry
*/
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addRedirectViewController("/api-docs","/swagger-ui.html");
}
}
Disable Method 3:
Use the annotation @ ConditionalOnProperty (name = "swagger. enable", havingValue = "true") and then add swagger. enable = true to the test configuration or development configuration to turn it on. If the production environment is not filled in, Swagger will be turned off by default.
package com.dc.config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* @author sunny chen
* @version V1.0
* @Package com.dc.config
* @date 2018/1/16 17:33
* @Description: Main purpose: Open online interface documents and add related configurations
*/
@Configuration
@EnableSwagger2
@ConditionalOnProperty(name ="enabled" ,prefix = "swagger",havingValue = "true",matchIfMissing = true)
public class Swagger2Config extends WebMvcConfigurerAdapter {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.dc.controller"))
.paths(PathSelectors.any())
//.paths(PathSelectors.none())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("auth System data interface document ")
.description(" This system is a new architecture Api Explanatory document ")
.termsOfServiceUrl("")
.contact(new Contact(" Chen Yongjia chen867647213@163.com", "", "https://blog.csdn.net/Mrs_chens"))
.version("1.0")
.build();
}
/**
* swagger ui Resource mapping
* @param registry
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
/**
* swagger-ui.html Path mapping, used in browsers /api-docs Visit
* @param registry
*/
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addRedirectViewController("/api-docs","/swagger-ui.html");
}
}