need:
In the production environment, you need to close the swagger configuration to avoid interface exposure.
method:
1. Use the annotation @Value()
2. Use the annotation @Profile({"dev", "test"}) to indicate that it is turned on in the development or test environment and turned off in production.
3. Use the annotation @ConditionalOnProperty(name = “swagger.enable”, havingValue = “true”) and then add swagger.enable = true in the test configuration or development configuration to enable it. If the production environment is not filled, Swagger is disabled by default.
Method 1: Use the annotation @Value()
Add it in the Swagger2Config class;
and you need to add a swagger.enable attribute in the configuration file, and dynamically insert true or false according to different application-xx.yml.
@Configuration
@EnableSwagger2
public class Swagger2Config {
@Value("${swagger.enable}")
private Boolean enable;
@Bean
public Docket swaggerPersonApi10() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.unidata.cloud.logservice.api.controller"))
.paths(PathSelectors.any())
.enable(enable) //配置在该处生效
.build()
.apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.version("1.0")
.title("xx项目:xx平台 Swagger2 文档 API")
.contact(new Contact(" xx团队", "https://www.xx.com/", "[email protected]"))
.description("logservice platform API v1.0")
.build();
}
}
Method 2: Use the annotation @Profile({"dev", "test"}) to indicate that it is enabled in the development or test environment and disabled in production.
@Configuration
@EnableSwagger2
@Profile({
"local", "dev"})
public class Swagger2Config {
@Bean
public Docket swaggerPersonApi10() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.unidata.cloud.logservice.api.controller"))
.paths(PathSelectors.any())
.build()
.apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.version("1.0")
.title("xx项目:xx平台 Swagger2 文档 API")
.contact(new Contact(" xx团队", "https://www.xx.com/", "[email protected]"))
.description("logservice platform API v1.0")
.build();
}
}
Method 3: Use the annotation @ConditionalOnProperty(name = “swagger.enable”, havingValue = “true”) and add swagger.enable = true in the test configuration or development configuration to enable it. If the production environment is not filled, Swagger is disabled by default.
1. Use the annotation @ConditionalOnProperty(name = “swagger.enable”, havingValue = “true”)
@Configuration
@EnableSwagger2
//@ConditionalOnProperty(name ="enabled" ,prefix = "swagger",havingValue = "true",matchIfMissing = true //matchIfMissing=true :为空则设为true,不合理
@ConditionalOnProperty(name = “swagger.enable”, havingValue = “true”)
public class Swagger2Config {
@Bean
public Docket swaggerPersonApi10() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.unidata.cloud.logservice.api.controller"))
.paths(PathSelectors.any())
.enable(enable)
.build()
.apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.version("1.0")
.title("xx项目:xx平台 Swagger2 文档 API")
.contact(new Contact(" xx团队", "https://www.xxx.com/", "[email protected]"))
.description("logservice platform API v1.0")
.build();
}
}
2. Then add swagger.enable = true in the test configuration or development configuration to enable it. If the configuration is not written in the production environment, Swagger will be disabled by default.
#Swagger lock
swagger:
enabled: true
Reference link: https://www.dandelioncloud.cn/article/details/1593427183718813697