SpringBoot线上环境彻底关闭Swagger-UI的方式
概要
Springboot线上环境彻底关闭Swagger-UI
整体架构流程
1.SwaggerConfig使用@Profile排除线上环境其他环境生效
2.创建一个控制类使用@Profile仅线上环境生效,使访问swagger-ui.html返回404
技术细节
/**
* @author: suitman
* @description: go fucking comment....
* @create: 2021-02-07 10:43
**/
@Configuration
@EnableSwagger2
@Profile("!prod")
public class SwaggerConfig implements WebMvcConfigurer {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(new ApiInfoBuilder()
// 设置标题
.title("****")
// 描述
.description("***")
// 作者信息
.contact(new Contact("***", null, null))
// 版本
.version("版本号: 1")
.build())
.select()
.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
.paths(PathSelectors.any())
.build();
}
@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/");
}
}
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Profile;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Profile("prod")
@RestController
@Slf4j
public class DisableSwaggerUiController {
@RequestMapping(value = "swagger-ui.html", method = RequestMethod.GET)
public void getSwagger(HttpServletResponse httpResponse) throws IOException {
httpResponse.setStatus(HttpStatus.NOT_FOUND.value());
}
}
小结
通过这种方式可以彻底关闭线上环境访问swagger-ui.html直接返回404
到此这篇关于SpringBoot线上环境彻底关闭Swagger-UI的方式的文章就介绍到这了,更多相关SpringBoot彻底关闭Swagger-UI内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
一文秒懂IDEA中每天都在用的Project Structure知识
这篇文章主要介绍了一文秒懂IDEA中每天都在用的Project Structure知识,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2020-10-10
详解Java使用Pipeline对Redis批量读写(hmset&hgetall)
本篇文章主要介绍了Java使用Pipeline对Redis批量读写(hmset&hgetall),具有一定的参考价值,有兴趣的可以了解一下。2016-12-12
Springboot3集成Knife4j的步骤以及使用(最完整版)
这篇文章主要介绍了Springboot3集成Knife4j的步骤以及使用的相关资料,Knife4j是一种增强Swagger的工具,支持黑色主题和更多配置选项,它与swagger-bootstrap-ui相比,提供了更现代的外观和更多的功能,需要的朋友可以参考下2024-11-11
centos下GitLab+Jenkins持续集成环境搭建(安装jenkins)
这篇文章主要为大家详细介绍了centos下搭建GitLab+Jenkins持续集成环境,安装jenkins的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2018-04-04
JAVA 聚焦 OutOfMemoryError 异常问题记录
在 Java 开发中,内存溢出异常是影响程序稳定性的关键问题,了解其原理和应对方法,对开发者至关重要,这篇文章主要介绍了JAVA聚焦 OutOfMemoryError 异常,需要的朋友可以参考下2025-04-04
java中为什么要谨慎使用Arrays.asList、ArrayList的subList
这篇文章主要介绍了java中为什么要谨慎使用Arrays.asList、ArrayList的subList,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2021-02-02


最新评论