Spring Boot 禁用 Swagger 的三种方式

举报
轻狂书生FS 发表于 2020/11/28 22:20:31 2020/11/28
【摘要】 一、序言 在生产环境下,我们需要关闭swagger配置,避免暴露接口的这种危险行为。 二、方法: 禁用方法1: 使用注解 @Value() 推荐使用 package com.dc.config; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;import o...

一、序言

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

二、方法:

禁用方法1:

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


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

禁用方法2:

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


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

禁用方法3:

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


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

 

 

 

文章来源: blog.csdn.net,作者:LookForDream_,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/LookForDream_/article/details/105073465

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。