版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_37591536/article/details/82115325
一、序言
在生产环境下,我们需要关闭swagger配置,避免暴露接口的这种危险行为。
二、方法:
禁用方法1:使用注解@Profile({"dev","test"})
表示在开发或测试环境开启,而在生产关闭。(推荐使用)
禁用方法2:使用注解@ConditionalOnProperty(name = "swagger.enable", havingValue = "true")
然后在测试配置或者开发配置中 添加 swagger.enable = true 即可开启,生产环境不填则默认关闭Swagger.
如方法1:
package com.left;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* <pre>
* @author : orange
* e-mail : [email protected]
* time : 2018/8/27 14:49
* desc : swagger配置
* version: 1.0
* </pre>
*/
@Configuration
@EnableSwagger2
@Profile({"dev","test"})
public class Swagger2 {
@Bean
public Docket restApi(){
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.left.controller"))
.paths(PathSelectors.any())
.build()
.apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder().title("springboot整合swagger2")
.description("springboot整合swagger2")
.termsOfServiceUrl("https://blog.csdn.net/weixin_37591536")
.version("1.0")
.build();
}
}
结果:
开发环境可以正常访问
生产环境: