在生产环境禁用Swagger2

场景

swagger 用来在开发阶段方便前后端分离的项目实战中,提高前后端人员的工作效率,降低交流成本。但是版本上线之后,要是把 swagger 带上去会存在很大的风险

禁用方法

基于 2.9.2
		<!-- swagger -->
		<dependency>
		<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger2</artifactId>
			<version>2.9.2</version>
		</dependency>
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger-ui</artifactId>
			<version>2.9.2</version>
		</dependency>
		<dependency>
		    <groupId>io.springfox</groupId>
		    <artifactId>springfox-spring-webmvc</artifactId>
		    <version>2.9.2</version>
		</dependency>
方法一(推荐)

在自定义的 Swagger2 配置类中,通过 @ConditionalOnProperty(prefix = “base”,name=“swagger-open”,havingValue = “true”)注解实现

@Configuration
@EnableSwagger2
@ConditionalOnProperty(prefix = "base",name="swagger-open",havingValue = "true")
public class Swagger2 {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }

	private ApiInfo apiInfo() {
	        return new ApiInfoBuilder()
	                .title("Spring Boot中使用Swagger2构建RESTful APIs")
	                .description("")
	                .termsOfServiceUrl("")
	                .contact(new Contact("亦安✘","http:www.xxx.com","xxx@xxx.com"))
	                .version("1.0")
	                .build();
	}
}

PS

  1. 在自定义的 Swagger2配置类中,通过 @ConditionalOnProperty(prefix = “base”,name=“swagger-open”,havingValue = “true”) 注解实现

  2. 读取配置文件中前缀为 base的配置,属性名为 swagger-open,只为 true

  3. 当条件成立,此配置类被激活

  4. 配置文件如下

###################  项目启动端口  ###################
server:
  	port: 8080
  	servlet:
    	context-path: /${base.project}-api

base:
	project: yian
  	filePath: /data/${base.project} #静态文件路径
  	swagger-open: true #是否启用swagger
方法二
  1. 在配置中编写配置文件
base:
	swagger-open: true #是否启用swagger
  1. 在自定义的 Swagger2 配置类中获取配置文件中的配置信息
@Configuration
@EnableSwagger2
public class Swagger2 {
  	@Value("${base.swagger-open}")
    private boolean swaggerSwitch;
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
        		.enable(swaggerSwitch)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Spring Boot中使用Swagger2构建RESTful APIs")
                .description("")
                .termsOfServiceUrl("")
                .contact(new Contact("亦安✘","http:www.xxx.com","xxx@xxx.com"))
                .version("1.0")
                .build();
    }

}
  • 10
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值