首页>>后端>>SpringBoot->Spring Boot禁用Swagger的三种方法

Spring Boot禁用Swagger的三种方法

时间:2023-11-29 本站 点击:1

在生产环境下,我们需要关闭swagger配置,避免暴露接口的这种危险行为。下面给大家分享关于Spring Boot禁用Swagger的3种方法,一起来学习下吧!

禁用方法1:

使用注解 @Value() 推荐使用

packagecom.dc.config;importorg.springframework.boot.autoconfigure.condition.ConditionalOnProperty;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importorg.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;importorg.springframework.web.servlet.config.annotation.ViewControllerRegistry;importorg.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;importspringfox.documentation.builders.ApiInfoBuilder;importspringfox.documentation.builders.PathSelectors;importspringfox.documentation.builders.RequestHandlerSelectors;importspringfox.documentation.service.ApiInfo;importspringfox.documentation.service.Contact;importspringfox.documentation.spi.DocumentationType;importspringfox.documentation.spring.web.plugins.Docket;importspringfox.documentation.swagger2.annotations.EnableSwagger2;/***@authorsunnychen*@versionV1.0*@Packagecom.dc.config*@date2018/1/1617:33*@Description:主要用途:开启在线接口文档和添加相关配置*/@Configuration@EnableSwagger2publicclassSwagger2ConfigextendsWebMvcConfigurerAdapter{@Value("${swagger.enable}")privateBooleanenable;@BeanpublicDocketcreateRestApi(){returnnewDocket(DocumentationType.SWAGGER_2).enable(enable).apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.basePackage("com.dc.controller")).paths(PathSelectors.any())//.paths(PathSelectors.none()).build();}privateApiInfoapiInfo(){returnnewApiInfoBuilder().title("auth系统数据接口文档").description("此系统为新架构Api说明文档").termsOfServiceUrl("").contact(newContact("陈永佳chen867647213@163.com","","https://blog.csdn.net/Mrs_chens")).version("1.0").build();}/***swaggerui资源映射*@paramregistry*/@OverridepublicvoidaddResourceHandlers(ResourceHandlerRegistryregistry){registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");}/***swagger-ui.html路径映射,浏览器中使用/api-docs访问*@paramregistry*/@OverridepublicvoidaddViewControllers(ViewControllerRegistryregistry){registry.addRedirectViewController("/api-docs","/swagger-ui.html");}}

禁用方法2:

使用注解 @Profile({“dev”,“test”}) 表示在开发或测试环境开启,而在生产关闭。(推荐使用)

packagecom.dc.config;importorg.springframework.boot.autoconfigure.condition.ConditionalOnProperty;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importorg.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;importorg.springframework.web.servlet.config.annotation.ViewControllerRegistry;importorg.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;importspringfox.documentation.builders.ApiInfoBuilder;importspringfox.documentation.builders.PathSelectors;importspringfox.documentation.builders.RequestHandlerSelectors;importspringfox.documentation.service.ApiInfo;importspringfox.documentation.service.Contact;importspringfox.documentation.spi.DocumentationType;importspringfox.documentation.spring.web.plugins.Docket;importspringfox.documentation.swagger2.annotations.EnableSwagger2;/***@authorsunnychen*@versionV1.0*@Packagecom.dc.config*@date2018/1/1617:33*@Description:主要用途:开启在线接口文档和添加相关配置*/@Configuration@EnableSwagger2@Profile({“dev”,“test”})publicclassSwagger2ConfigextendsWebMvcConfigurerAdapter{@BeanpublicDocketcreateRestApi(){returnnewDocket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.basePackage("com.dc.controller")).paths(PathSelectors.any())//.paths(PathSelectors.none()).build();}privateApiInfoapiInfo(){returnnewApiInfoBuilder().title("auth系统数据接口文档").description("此系统为新架构Api说明文档").termsOfServiceUrl("").contact(newContact("陈永佳chen867647213@163.com","","https://blog.csdn.net/Mrs_chens")).version("1.0").build();}/***swaggerui资源映射*@paramregistry*/@OverridepublicvoidaddResourceHandlers(ResourceHandlerRegistryregistry){registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");}/***swagger-ui.html路径映射,浏览器中使用/api-docs访问*@paramregistry*/@OverridepublicvoidaddViewControllers(ViewControllerRegistryregistry){registry.addRedirectViewController("/api-docs","/swagger-ui.html");}}

禁用方法3:

使用注解 @ConditionalOnProperty(name = “swagger.enable”, havingValue = “true”) 然后在测试配置或者开发配置中 添加 swagger.enable = true 即可开启,生产环境不填则默认关闭 Swagger.

packagecom.dc.config;importorg.springframework.boot.autoconfigure.condition.ConditionalOnProperty;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importorg.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;importorg.springframework.web.servlet.config.annotation.ViewControllerRegistry;importorg.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;importspringfox.documentation.builders.ApiInfoBuilder;importspringfox.documentation.builders.PathSelectors;importspringfox.documentation.builders.RequestHandlerSelectors;importspringfox.documentation.service.ApiInfo;importspringfox.documentation.service.Contact;importspringfox.documentation.spi.DocumentationType;importspringfox.documentation.spring.web.plugins.Docket;importspringfox.documentation.swagger2.annotations.EnableSwagger2;/***@authorsunnychen*@versionV1.0*@Packagecom.dc.config*@date2018/1/1617:33*@Description:主要用途:开启在线接口文档和添加相关配置*/@Configuration@EnableSwagger2@ConditionalOnProperty(name="enabled",prefix="swagger",havingValue="true",matchIfMissing=true)publicclassSwagger2ConfigextendsWebMvcConfigurerAdapter{@BeanpublicDocketcreateRestApi(){returnnewDocket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.basePackage("com.dc.controller")).paths(PathSelectors.any())//.paths(PathSelectors.none()).build();}privateApiInfoapiInfo(){returnnewApiInfoBuilder().title("auth系统数据接口文档").description("此系统为新架构Api说明文档").termsOfServiceUrl("").contact(newContact("陈永佳chen867647213@163.com","","https://blog.csdn.net/Mrs_chens")).version("1.0").build();}/***swaggerui资源映射*@paramregistry*/@OverridepublicvoidaddResourceHandlers(ResourceHandlerRegistryregistry){registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");}/***swagger-ui.html路径映射,浏览器中使用/api-docs访问*@paramregistry*/@OverridepublicvoidaddViewControllers(ViewControllerRegistryregistry){registry.addRedirectViewController("/api-docs","/swagger-ui.html");}}
#Swaggerlockswagger:enabled:true

最后感谢大家耐心观看完毕,原创不易,留个点赞收藏是您对我最大的鼓励!

作者:Sunny_Chen


本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:/SpringBoot/363.html