转自:https://blog.csdn.net/weixin_37264997/article/details/82762050

一、序言

在生产环境下,我们需要关闭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 zhaohp
  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.  
  32. @Bean
  33. public Docket createRestApi() {
  34. return new Docket(DocumentationType.SWAGGER_2)
  35. .enable(enable)
  36. .apiInfo(apiInfo())
  37. .select()
  38. .apis(RequestHandlerSelectors.basePackage("com.dc.controller"))
  39. .paths(PathSelectors.any())
  40. //.paths(PathSelectors.none())
  41. .build();
  42. }
  43.  
  44. private ApiInfo apiInfo() {
  45. return new ApiInfoBuilder()
  46. .title("auth系统数据接口文档")
  47. .description("此系统为新架构Api说明文档")
  48. .termsOfServiceUrl("")
  49. .contact(new Contact("赵化鹏 18310695431@163.com", "", "zhaohuapeng@zichan360.com"))
  50. .version("1.0")
  51. .build();
  52. }
  53.  
  54. /**
  55. * swagger ui资源映射
  56. * @param registry
  57. */
  58. @Override
  59. public void addResourceHandlers(ResourceHandlerRegistry registry) {
  60. registry.addResourceHandler("swagger-ui.html")
  61. .addResourceLocations("classpath:/META-INF/resources/");
  62.  
  63. registry.addResourceHandler("/webjars/**")
  64. .addResourceLocations("classpath:/META-INF/resources/webjars/");
  65. }
  66.  
  67. /**
  68. * swagger-ui.html路径映射,浏览器中使用/api-docs访问
  69. * @param registry
  70. */
  71. @Override
  72. public void addViewControllers(ViewControllerRegistry registry) {
  73. registry.addRedirectViewController("/api-docs","/swagger-ui.html");
  74. }
  75. }

禁用方法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 zhaohp
  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("赵化鹏 18310695431@163.com", "", "zhaohuapeng@zichan360.com"))
  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.

关键就是这里的 @ConditionalOnProperty

这里的属性key是 swagger.enable ,havingValue 是期望值,只有在值等于期望值的时候,才会生效。也就是说,swagger.enable只能为true的时候才会生效,其他值或不设值,都不会生效的。

  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 zhaohp
  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("赵化鹏 18310695431@163.com", "", "zhaohuapeng@zichan360.com"))
  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的更多相关文章

  1. 【简记】SpringBoot禁用Swagger

    楔子 Swagger 是 Java Web 开发中常用的接口文档生成类库,在开发和前后端联调时使用它来模拟接口调用能提高开发效率.但是,在生产环境可能并不需要它,一个原因是启用它会延长程序启动时间(动 ...

  2. 转-spring-boot 注解配置mybatis+druid(新手上路)-http://blog.csdn.net/sinat_36203615/article/details/53759935

    spring-boot 注解配置mybatis+druid(新手上路) 转载 2016年12月20日 10:17:17 标签: sprinb-boot / mybatis / druid 10475 ...

  3. 如何在生产环境禁用swagger

    pringMVC集成springfox-swagger2和springfox-swagger-ui很简单,只需要两步: (1)pom中添加依赖 <dependency> <group ...

  4. 「快学springboot」16.让swagger帮忙写接口文档

    swagger简介 官方的介绍 THE WORLD'S MOST POPULAR API TOOLING Swagger is the world's largest framework of API ...

  5. jackson学习之十(终篇):springboot整合(配置类)

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  6. SpringBoot常用配置简介

    SpringBoot常用配置简介 1. SpringBoot中几个常用的配置的简单介绍 一个简单的Spring.factories # Bootstrap components org.springf ...

  7. 在SpringBoot中配置aop

    前言 aop作为spring的一个强大的功能经常被使用,aop的应用场景有很多,但是实际的应用还是需要根据实际的业务来进行实现.这里就以打印日志作为例子,在SpringBoot中配置aop 已经加入我 ...

  8. SpringBoot cache-control 配置静态资源缓存 (以及其中的思考经历)

    昨天在部署项目时遇到一个问题,因为服务要部署到外网使用,中间经过了较多的网络传输限制,而且要加载arcgis等较大的文件,所以在部署后,发现页面loading需要很长时间,而且刷新也要重新从服务器下载 ...

  9. 补习系列(10)-springboot 之配置读取

    目录 简介 一.配置样例 二.如何注入配置 1. 缺省配置文件 2. 使用注解 3. 启动参数 还有.. 三.如何读取配置 @Value 注解 Environment 接口 @Configuratio ...

随机推荐

  1. 在VMMap中跟踪不可用的虚拟内存

    VMMap是一个很好的系统内部工具,它可以可视化特定进程的虚拟内存,并帮助理解内存的用途.它有线程堆栈.映像.Win32堆和GC堆的特定报告.有时,VMMap会报告不可用的虚拟内存,这与可用内存不同. ...

  2. IIS 加载字体

    原文:https://blog.csdn.net/prospertu/article/details/72852500 <system.webServer> <staticConte ...

  3. Hibernate 关联关系(一对多)

    Hibernate 关联关系(一对多) 1. 什么是关联(association) 1.1 关联指的是类之间的引用关系.如果类A与类B关联,那么被引用的类B将被定义为类A的属性.例如: class B ...

  4. 洛谷p1458顺序的分数题解

    抱歉,您们的蒟蒻yxj不知道怎么插入链接qwq就只好粘个文本的了qwq:https://www.luogu.org/problemnew/show/P1458 没错,是个黄题,因为你们的小蒟蒻只会这样 ...

  5. eclipse中自动生成serialVersionUID

     serialVersionUID作用:  序列化时为了保持版本的兼容性,即在版本升级时反序列化仍保持对象的唯一性.       如果你修改代码重新部署后出现序列化错误,可以考虑给相应的类增加seri ...

  6. Spring Security教程之退出登录logout(十)

    要实现退出登录的功能我们需要在http元素下定义logout元素,这样Spring Security将自动为我们添加用于处理退出登录的过滤器LogoutFilter到FilterChain.当我们指定 ...

  7. docker 挂载主机目录 -v 和 --mount区别

    使用-v  时,如果宿主机上没有这个文件,也会自动创建, 但是如果使用--mount时,宿主机中没有这个文件会报错找不到这个文件,并创建失败

  8. python 散点图上给每个点打标签方便看到数据

    import numpy as np import matplotlib.pyplot as plt x=[2.3,4.5,3,7,6.5,4,5.3] y=[5,4,7,5,5.3,5.5,6.2] ...

  9. 《Linux就该这么学》培训笔记_ch03_管道符、重定向与环境变量

    <Linux就该这么学>培训笔记_ch03_管道符.重定向与环境变量 文章最后会post上书本的笔记照片. 文章主要内容: 输入输出重定向 管道命令符 命令行的通配符 常用的转义字符 重要 ...

  10. Debug 路漫漫-09:构建CNN时维度不一致问题

    Build CNN Network 之后,运行,但是报错: ValueError: Input 0 is incompatible with layer predict_vector_conv1: e ...