SpringBoot线上环境彻底关闭Swagger-UI的方式
更新时间:2023年12月20日 11:26:12 作者:阳夏丨
这篇文章主要给大家介绍了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内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
详解springboot+mybatis多数据源最简解决方案
本篇文章主要介绍了详解springboot+mybatis多数据源最简解决方案,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2017-05-05
使用Java解析JSON数据并提取特定字段的实现步骤(以提取mailNo为例)
在现代软件开发中,处理JSON数据是一项非常常见的任务,无论是从API接口获取数据,还是将数据存储为JSON格式,解析和提取JSON中的特定字段都是开发人员需要掌握的基本技能,本文将以一个实际案例为例,详细介绍如何使用Java解析JSON数据并提取其中的mailNo字段2025-01-01


最新评论