[转]SpringBoot整合Swagger2以及生产环境的安全问题处理
1.创建springboot项目
https://www.cnblogs.com/i-tao/p/8878562.html
这里我们使用多环境配置:
- application-dev.yml(开发环境)
- application-test.yml(测试环境)
- application-uat.yml(预发布)
- application-pro.yml(生产环境)
2.添加Swagger2依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
2.1 启动类开启Swagger2
package com.tao.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import springfox.documentation.swagger2.annotations.EnableSwagger2; @SpringBootApplication @EnableSwagger2 public class SpringbootApplication { public static void main(String[] args) { SpringApplication.run(SpringbootApplication.class, args); } }
2.2 Swagger2配置类
package com.tao.springboot.util; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; @Configuration public class Swagger2 { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.tao.springboot.action"))//controller路径 .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("标题") .description("描述") .termsOfServiceUrl("地址") .version("1.0") .build(); } }
2.3 开始在action里面写一个接口
package com.tao.springboot.action;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(value = "/says",method = RequestMethod.GET)
public class sayHello {
/**
* 根据用户名说hello
* @param name
* @return
*/
@RequestMapping(value = "/hello",method = RequestMethod.GET)
public String hello(String name){
return name+" hello";
}
}
为了方便接口管理和维护,增加Swagger2注解:
- @Api:修饰整个类,描述Controller的作用
- @ApiOperation:描述一个类的一个方法,或者说一个接口
- @ApiParam:单个参数描述
- @ApiModel:用对象来接收参数
- @ApiProperty:用对象接收参数时,描述对象的一个字段
- @ApiResponse:HTTP响应其中1个描述
- @ApiResponses:HTTP响应整体描述
- @ApiIgnore:使用该注解忽略这个API
- @ApiError :发生错误返回的信息
- @ApiImplicitParam:一个请求参数
- @ApiImplicitParams:多个请求参数
package com.tao.springboot.action; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiOperation; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping(value = "/says",method = RequestMethod.GET) public class sayHello { /** * 根据用户名说hello * @param name * @return */ @ApiOperation(value="say hello", notes="根据url的name来say hello!") @ApiImplicitParam(name = "name", value = "用户名称", required = true, dataType = "String", paramType = "path") @RequestMapping(value = "/hello",method = RequestMethod.GET) public String hello(String name){ return name+" hello"; } }
访问:http://localhost:8081/swagger-ui.html
3.如果解决线上接口不被暴露?
3.1 使用springboot security过滤
略……
3.2 生产环境移除Swagger2
略……
3.3 直接使用多环境配置,生产环境不启用Swagger2
application.yml文件
spring:
profiles:
active: pro
application-pro.yml
#生产环境 server: port: 8080 swagger2: enable: false
2.2 Swagger2配置类增加
@Value("${swagger2.enable}") private boolean swagger2Enable;
package com.tao.springboot.util; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; @Configuration public class Swagger2 { @Value("${swagger2.enable}") private boolean swagger2Enable; @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .enable(swagger2Enable) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.tao.springboot.action"))//controller路径 .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("标题") .description("描述") .termsOfServiceUrl("地址") .version("1.0") .build(); } }
访问:http://localhost:8081/swagger-ui.html
访问:http://localhost:8080/swagger-ui.html
github地址:https://github.com/80905949/springbootswagger2.git
---------------------
作者:缘故为何
来源:CNBLOGS
原文:https://www.cnblogs.com/i-tao/p/10548181.html
版权声明:本文为作者原创文章,转载请附上博文链接!
内容解析By:CSDN,CNBLOG博客文章一键转载插件
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构