您好,登錄后才能下訂單哦!
這篇文章主要介紹“SpringBoot禁用Swagger的方式有哪些”,在日常操作中,相信很多人在SpringBoot禁用Swagger的方式有哪些問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”SpringBoot禁用Swagger的方式有哪些”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
在生產環境下,我們需要關閉swagger配置,避免暴露接口的這種危險行為。
使用注解 @Value() 推薦使用
package com.dc.config; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.service.Contact; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; /** * @author sunny chen * @version V1.0 * @Package com.dc.config * @date 2018/1/16 17:33 * @Description: 主要用途:開啟在線接口文檔和添加相關配置 */ @Configuration @EnableSwagger2 public class Swagger2Config extends WebMvcConfigurerAdapter { @Value("${swagger.enable}") private Boolean enable; @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .enable(enable) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.dc.controller")) .paths(PathSelectors.any()) //.paths(PathSelectors.none()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("auth系統數據接口文檔") .description("此系統為新架構Api說明文檔") .termsOfServiceUrl("") .contact(new Contact("陳永佳 chen867647213@163.com", "", "https://blog.csdn.net/Mrs_chens")) .version("1.0") .build(); } /** * swagger ui資源映射 * @param registry */ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("swagger-ui.html") .addResourceLocations("classpath:/META-INF/resources/"); registry.addResourceHandler("/webjars/**") .addResourceLocations("classpath:/META-INF/resources/webjars/"); } /** * swagger-ui.html路徑映射,瀏覽器中使用/api-docs訪問 * @param registry */ @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addRedirectViewController("/api-docs","/swagger-ui.html"); } }
使用注解 @Profile({“dev”,“test”}) 表示在開發或測試環境開啟,而在生產關閉。(推薦使用)
package com.dc.config; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.service.Contact; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; /** * @author sunny chen * @version V1.0 * @Package com.dc.config * @date 2018/1/16 17:33 * @Description: 主要用途:開啟在線接口文檔和添加相關配置 */ @Configuration @EnableSwagger2 @Profile({“dev”,“test”}) public class Swagger2Config extends WebMvcConfigurerAdapter { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.dc.controller")) .paths(PathSelectors.any()) //.paths(PathSelectors.none()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("auth系統數據接口文檔") .description("此系統為新架構Api說明文檔") .termsOfServiceUrl("") .contact(new Contact("陳永佳 chen867647213@163.com", "", "https://blog.csdn.net/Mrs_chens")) .version("1.0") .build(); } /** * swagger ui資源映射 * @param registry */ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("swagger-ui.html") .addResourceLocations("classpath:/META-INF/resources/"); registry.addResourceHandler("/webjars/**") .addResourceLocations("classpath:/META-INF/resources/webjars/"); } /** * swagger-ui.html路徑映射,瀏覽器中使用/api-docs訪問 * @param registry */ @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addRedirectViewController("/api-docs","/swagger-ui.html"); } }
使用注解 @ConditionalOnProperty(name = “swagger.enable”, havingValue = “true”) 然后在測試配置或者開發配置中 添加 swagger.enable = true 即可開啟,生產環境不填則默認關閉 Swagger.
package com.dc.config; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.service.Contact; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; /** * @author sunny chen * @version V1.0 * @Package com.dc.config * @date 2018/1/16 17:33 * @Description: 主要用途:開啟在線接口文檔和添加相關配置 */ @Configuration @EnableSwagger2 @ConditionalOnProperty(name ="enabled" ,prefix = "swagger",havingValue = "true",matchIfMissing = true) public class Swagger2Config extends WebMvcConfigurerAdapter { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.dc.controller")) .paths(PathSelectors.any()) //.paths(PathSelectors.none()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("auth系統數據接口文檔") .description("此系統為新架構Api說明文檔") .termsOfServiceUrl("") .contact(new Contact("陳永佳 chen867647213@163.com", "", "https://blog.csdn.net/Mrs_chens")) .version("1.0") .build(); } /** * swagger ui資源映射 * @param registry */ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("swagger-ui.html") .addResourceLocations("classpath:/META-INF/resources/"); registry.addResourceHandler("/webjars/**") .addResourceLocations("classpath:/META-INF/resources/webjars/"); } /** * swagger-ui.html路徑映射,瀏覽器中使用/api-docs訪問 * @param registry */ @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addRedirectViewController("/api-docs","/swagger-ui.html"); } }
到此,關于“SpringBoot禁用Swagger的方式有哪些”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。