经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Spring » 查看文章
详解SpringBoot禁用Swagger的三种方式
来源:jb51  时间:2021/11/22 13:41:51  对本文有异议

摘要

在生产环境下,我们需要关闭swagger配置,避免暴露接口的这种危险行为。

方法

禁用方法1:

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

  1. package com.dc.config;
  2.  
  3. import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
  7. import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
  8. import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
  9. import springfox.documentation.builders.ApiInfoBuilder;
  10. import springfox.documentation.builders.PathSelectors;
  11. import springfox.documentation.builders.RequestHandlerSelectors;
  12. import springfox.documentation.service.ApiInfo;
  13. import springfox.documentation.service.Contact;
  14. import springfox.documentation.spi.DocumentationType;
  15. import springfox.documentation.spring.web.plugins.Docket;
  16. import springfox.documentation.swagger2.annotations.EnableSwagger2;
  17.  
  18. /**
  19. * @author sunny chen
  20. * @version V1.0
  21. * @Package com.dc.config
  22. * @date 2018/1/16 17:33
  23. * @Description: 主要用途:开启在线接口文档和添加相关配置
  24. */
  25. @Configuration
  26. @EnableSwagger2
  27. public class Swagger2Config extends WebMvcConfigurerAdapter {
  28.  
  29. @Value("${swagger.enable}")
  30. private Boolean enable;
  31. @Bean
  32. public Docket createRestApi() {
  33. return new Docket(DocumentationType.SWAGGER_2)
  34. .enable(enable)
  35. .apiInfo(apiInfo())
  36. .select()
  37. .apis(RequestHandlerSelectors.basePackage("com.dc.controller"))
  38. .paths(PathSelectors.any())
  39. //.paths(PathSelectors.none())
  40. .build();
  41. }
  42.  
  43. private ApiInfo apiInfo() {
  44. return new ApiInfoBuilder()
  45. .title("auth系统数据接口文档")
  46. .description("此系统为新架构Api说明文档")
  47. .termsOfServiceUrl("")
  48. .contact(new Contact("陈永佳 chen867647213@163.com", "", "https://blog.csdn.net/Mrs_chens"))
  49. .version("1.0")
  50. .build();
  51. }
  52.  
  53. /**
  54. * swagger ui资源映射
  55. * @param registry
  56. */
  57. @Override
  58. public void addResourceHandlers(ResourceHandlerRegistry registry) {
  59. registry.addResourceHandler("swagger-ui.html")
  60. .addResourceLocations("classpath:/META-INF/resources/");
  61.  
  62. registry.addResourceHandler("/webjars/**")
  63. .addResourceLocations("classpath:/META-INF/resources/webjars/");
  64. }
  65.  
  66. /**
  67. * swagger-ui.html路径映射,浏览器中使用/api-docs访问
  68. * @param registry
  69. */
  70. @Override
  71. public void addViewControllers(ViewControllerRegistry registry) {
  72. registry.addRedirectViewController("/api-docs","/swagger-ui.html");
  73. }
  74. }

禁用方法2:

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

  1. package com.dc.config;
  2.  
  3. import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
  7. import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
  8. import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
  9. import springfox.documentation.builders.ApiInfoBuilder;
  10. import springfox.documentation.builders.PathSelectors;
  11. import springfox.documentation.builders.RequestHandlerSelectors;
  12. import springfox.documentation.service.ApiInfo;
  13. import springfox.documentation.service.Contact;
  14. import springfox.documentation.spi.DocumentationType;
  15. import springfox.documentation.spring.web.plugins.Docket;
  16. import springfox.documentation.swagger2.annotations.EnableSwagger2;
  17.  
  18. /**
  19. * @author sunny chen
  20. * @version V1.0
  21. * @Package com.dc.config
  22. * @date 2018/1/16 17:33
  23. * @Description: 主要用途:开启在线接口文档和添加相关配置
  24. */
  25. @Configuration
  26. @EnableSwagger2
  27. @Profile({“dev”,“test”})
  28. public class Swagger2Config extends WebMvcConfigurerAdapter {
  29.  
  30. @Bean
  31. public Docket createRestApi() {
  32. return new Docket(DocumentationType.SWAGGER_2)
  33. .apiInfo(apiInfo())
  34. .select()
  35. .apis(RequestHandlerSelectors.basePackage("com.dc.controller"))
  36. .paths(PathSelectors.any())
  37. //.paths(PathSelectors.none())
  38. .build();
  39. }
  40.  
  41. private ApiInfo apiInfo() {
  42. return new ApiInfoBuilder()
  43. .title("auth系统数据接口文档")
  44. .description("此系统为新架构Api说明文档")
  45. .termsOfServiceUrl("")
  46. .contact(new Contact("陈永佳 chen867647213@163.com", "", "https://blog.csdn.net/Mrs_chens"))
  47. .version("1.0")
  48. .build();
  49. }
  50.  
  51. /**
  52. * swagger ui资源映射
  53. * @param registry
  54. */
  55. @Override
  56. public void addResourceHandlers(ResourceHandlerRegistry registry) {
  57. registry.addResourceHandler("swagger-ui.html")
  58. .addResourceLocations("classpath:/META-INF/resources/");
  59.  
  60. registry.addResourceHandler("/webjars/**")
  61. .addResourceLocations("classpath:/META-INF/resources/webjars/");
  62. }
  63.  
  64. /**
  65. * swagger-ui.html路径映射,浏览器中使用/api-docs访问
  66. * @param registry
  67. */
  68. @Override
  69. public void addViewControllers(ViewControllerRegistry registry) {
  70. registry.addRedirectViewController("/api-docs","/swagger-ui.html");
  71. }
  72. }

禁用方法3:

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

  1. package com.dc.config;
  2.  
  3. import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
  7. import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
  8. import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
  9. import springfox.documentation.builders.ApiInfoBuilder;
  10. import springfox.documentation.builders.PathSelectors;
  11. import springfox.documentation.builders.RequestHandlerSelectors;
  12. import springfox.documentation.service.ApiInfo;
  13. import springfox.documentation.service.Contact;
  14. import springfox.documentation.spi.DocumentationType;
  15. import springfox.documentation.spring.web.plugins.Docket;
  16. import springfox.documentation.swagger2.annotations.EnableSwagger2;
  17.  
  18. /**
  19. * @author sunny chen
  20. * @version V1.0
  21. * @Package com.dc.config
  22. * @date 2018/1/16 17:33
  23. * @Description: 主要用途:开启在线接口文档和添加相关配置
  24. */
  25. @Configuration
  26. @EnableSwagger2
  27. @ConditionalOnProperty(name ="enabled" ,prefix = "swagger",havingValue = "true",matchIfMissing = true)
  28. public class Swagger2Config extends WebMvcConfigurerAdapter {
  29.  
  30. @Bean
  31. public Docket createRestApi() {
  32. return new Docket(DocumentationType.SWAGGER_2)
  33. .apiInfo(apiInfo())
  34. .select()
  35. .apis(RequestHandlerSelectors.basePackage("com.dc.controller"))
  36. .paths(PathSelectors.any())
  37. //.paths(PathSelectors.none())
  38. .build();
  39. }
  40.  
  41. private ApiInfo apiInfo() {
  42. return new ApiInfoBuilder()
  43. .title("auth系统数据接口文档")
  44. .description("此系统为新架构Api说明文档")
  45. .termsOfServiceUrl("")
  46. .contact(new Contact("陈永佳 chen867647213@163.com", "", "https://blog.csdn.net/Mrs_chens"))
  47. .version("1.0")
  48. .build();
  49. }
  50.  
  51. /**
  52. * swagger ui资源映射
  53. * @param registry
  54. */
  55. @Override
  56. public void addResourceHandlers(ResourceHandlerRegistry registry) {
  57. registry.addResourceHandler("swagger-ui.html")
  58. .addResourceLocations("classpath:/META-INF/resources/");
  59.  
  60. registry.addResourceHandler("/webjars/**")
  61. .addResourceLocations("classpath:/META-INF/resources/webjars/");
  62. }
  63.  
  64. /**
  65. * swagger-ui.html路径映射,浏览器中使用/api-docs访问
  66. * @param registry
  67. */
  68. @Override
  69. public void addViewControllers(ViewControllerRegistry registry) {
  70. registry.addRedirectViewController("/api-docs","/swagger-ui.html");
  71. }
  72. }

到此这篇关于详解SpringBoot禁用Swagger的三种方式的文章就介绍到这了,更多相关SpringBoot禁用Swagger内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号