千家信息网

生产环境下springboot中怎么配置禁用swagger

发表于:2024-10-01 作者:千家信息网编辑
千家信息网最后更新 2024年10月01日,这篇文章主要讲解了"生产环境下springboot中怎么配置禁用swagger",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"生产环境下springbo
千家信息网最后更新 2024年10月01日生产环境下springboot中怎么配置禁用swagger

这篇文章主要讲解了"生产环境下springboot中怎么配置禁用swagger",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"生产环境下springboot中怎么配置禁用swagger"吧!

一、序言
在生产环境下,我们需要关闭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;

/**
*


* @author : orange
* e-mail : 495314527@qq.com
* time : 2018/8/27 14:49
* desc : swagger配置
* version: 1.0
*

*/
@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();
}

}
结果:

开发环境可以正常访问

感谢各位的阅读,以上就是"生产环境下springboot中怎么配置禁用swagger"的内容了,经过本文的学习后,相信大家对生产环境下springboot中怎么配置禁用swagger这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!

0