Springboot配置Swagger2登录密码的实现
Swagger
Swagger是使用OpenAPI规范(OAS)开发API的最广泛使用的工具生态系统。Swagger由开源和专业工具组成,满足几乎所有的需求和用例。
一、配置Swagger
添加依赖
// web依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> //swagger依赖 <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency>
添加配置类
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi()
{
return new Docket(DocumentationType.SWAGGER_2)
.groupName("")
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("包名"))
.paths(PathSelectors.any())
.build();
}
public ApiInfo apiInfo()
{
return new ApiInfoBuilder()
.title("接口")
.description("接口说明")
.version("1.0")
.build();
}
}使用
// 控制层
@Api(tags = "基础模块")
@RestController
@RequestMapping("/base")
public class BaseController {
@ApiOperation(value = "查询")
@RequestMapping(value = "/findList", method = RequestMethod.POST)
public RestResponse findList(@RequestBody Param param)
{
return RestResponse.ok();
}
}访问地址
localhost:8080/swagger-ui.html
将接口文档暴露在外网会出现一定的安全问题,此时我们需要给Swagger文档配置登录密码。
二、配置Swagger登录密码
添加依赖
<dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>swagger-bootstrap-ui</artifactId> <version>1.9.3</version> </dependency>
更新配置类
@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI //添加注解
public class SwaggerConfig {
}添加启动类注解
@EnableSwagger2
配置yaml文件
swagger: basic: enable: true // 启用 username: 用户名 password: 密码
到此这篇关于Springboot配置Swagger2登录密码的实现的文章就介绍到这了,更多相关Springboot Swagger2登录密码内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
Java中的CopyOnWriteArrayList你了解吗
CopyOnWriteArrayList是Java集合框架中的一种线程安全的List实现,这篇文章主要来和大家聊聊CopyOnWriteArrayList的简单使用,需要的可以参考一下2023-06-06
IDEA基于支付宝小程序搭建springboot项目的详细步骤
这篇文章主要介绍了IDEA基于支付宝小程序搭建springboot项目的详细步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧2019-04-04
IntelliJ IDEA 2023版本创建Spring项目时Java只能选择17或21的问题解决方法
spring-boot是一个基于Java的开源框架,用于快速构建生产级别的应用程序,这篇文章主要给大家介绍了关于IntelliJ IDEA 2023版本创建Spring项目时Java只能选择17或21的问题解决方法,需要的朋友可以参考下2024-07-07
SpringBoot使用Sa-Token实现路径拦截和特定接口放行
这篇文章主要介绍了SpringBoot使用Sa-Token实现路径拦截和特定接口放行,文中通过代码示例讲解的非常详细,对大家的学习或工作有一定的帮助,需要的朋友可以参考下2024-06-06


最新评论