SpringBoot 通过配置禁用swagger
转自:https://blog.csdn.net/weixin_37264997/article/details/82762050
一、序言
在生产环境下,我们需要关闭swagger配置,避免暴露接口的这种危险行为。
二、方法:
禁用方法1:
使用注解 @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 zhaohp
- * @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("赵化鹏 18310695431@163.com", "", "zhaohuapeng@zichan360.com"))
- .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");
- }
- }
禁用方法2:
使用注解@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 zhaohp
- * @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("赵化鹏 18310695431@163.com", "", "zhaohuapeng@zichan360.com"))
- .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");
- }
- }
禁用方法3:
使用注解@ConditionalOnProperty(name = “swagger.enable”, havingValue = “true”) 然后在测试配置或者开发配置中 添加 swagger.enable = true 即可开启,生产环境不填则默认关闭Swagger.
关键就是这里的 @ConditionalOnProperty
这里的属性key是 swagger.enable ,havingValue 是期望值,只有在值等于期望值的时候,才会生效。也就是说,swagger.enable只能为true的时候才会生效,其他值或不设值,都不会生效的。
- 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 zhaohp
- * @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("赵化鹏 18310695431@163.com", "", "zhaohuapeng@zichan360.com"))
- .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的更多相关文章
- 【简记】SpringBoot禁用Swagger
楔子 Swagger 是 Java Web 开发中常用的接口文档生成类库,在开发和前后端联调时使用它来模拟接口调用能提高开发效率.但是,在生产环境可能并不需要它,一个原因是启用它会延长程序启动时间(动 ...
- 转-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 ...
- 如何在生产环境禁用swagger
pringMVC集成springfox-swagger2和springfox-swagger-ui很简单,只需要两步: (1)pom中添加依赖 <dependency> <group ...
- 「快学springboot」16.让swagger帮忙写接口文档
swagger简介 官方的介绍 THE WORLD'S MOST POPULAR API TOOLING Swagger is the world's largest framework of API ...
- jackson学习之十(终篇):springboot整合(配置类)
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...
- SpringBoot常用配置简介
SpringBoot常用配置简介 1. SpringBoot中几个常用的配置的简单介绍 一个简单的Spring.factories # Bootstrap components org.springf ...
- 在SpringBoot中配置aop
前言 aop作为spring的一个强大的功能经常被使用,aop的应用场景有很多,但是实际的应用还是需要根据实际的业务来进行实现.这里就以打印日志作为例子,在SpringBoot中配置aop 已经加入我 ...
- SpringBoot cache-control 配置静态资源缓存 (以及其中的思考经历)
昨天在部署项目时遇到一个问题,因为服务要部署到外网使用,中间经过了较多的网络传输限制,而且要加载arcgis等较大的文件,所以在部署后,发现页面loading需要很长时间,而且刷新也要重新从服务器下载 ...
- 补习系列(10)-springboot 之配置读取
目录 简介 一.配置样例 二.如何注入配置 1. 缺省配置文件 2. 使用注解 3. 启动参数 还有.. 三.如何读取配置 @Value 注解 Environment 接口 @Configuratio ...
随机推荐
- 在VMMap中跟踪不可用的虚拟内存
VMMap是一个很好的系统内部工具,它可以可视化特定进程的虚拟内存,并帮助理解内存的用途.它有线程堆栈.映像.Win32堆和GC堆的特定报告.有时,VMMap会报告不可用的虚拟内存,这与可用内存不同. ...
- IIS 加载字体
原文:https://blog.csdn.net/prospertu/article/details/72852500 <system.webServer> <staticConte ...
- Hibernate 关联关系(一对多)
Hibernate 关联关系(一对多) 1. 什么是关联(association) 1.1 关联指的是类之间的引用关系.如果类A与类B关联,那么被引用的类B将被定义为类A的属性.例如: class B ...
- 洛谷p1458顺序的分数题解
抱歉,您们的蒟蒻yxj不知道怎么插入链接qwq就只好粘个文本的了qwq:https://www.luogu.org/problemnew/show/P1458 没错,是个黄题,因为你们的小蒟蒻只会这样 ...
- eclipse中自动生成serialVersionUID
serialVersionUID作用: 序列化时为了保持版本的兼容性,即在版本升级时反序列化仍保持对象的唯一性. 如果你修改代码重新部署后出现序列化错误,可以考虑给相应的类增加seri ...
- Spring Security教程之退出登录logout(十)
要实现退出登录的功能我们需要在http元素下定义logout元素,这样Spring Security将自动为我们添加用于处理退出登录的过滤器LogoutFilter到FilterChain.当我们指定 ...
- docker 挂载主机目录 -v 和 --mount区别
使用-v 时,如果宿主机上没有这个文件,也会自动创建, 但是如果使用--mount时,宿主机中没有这个文件会报错找不到这个文件,并创建失败
- 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] ...
- 《Linux就该这么学》培训笔记_ch03_管道符、重定向与环境变量
<Linux就该这么学>培训笔记_ch03_管道符.重定向与环境变量 文章最后会post上书本的笔记照片. 文章主要内容: 输入输出重定向 管道命令符 命令行的通配符 常用的转义字符 重要 ...
- Debug 路漫漫-09:构建CNN时维度不一致问题
Build CNN Network 之后,运行,但是报错: ValueError: Input 0 is incompatible with layer predict_vector_conv1: e ...