一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

Springboot配置Swagger2登录密码实现代码

时间:2022-03-11 编辑:袖梨 来源:一聚教程网

本篇文章小编给大家分享一下Springboot配置Swagger2登录密码实现代码,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。

Swagger

Swagger是使用OpenAPI规范(OAS)开发API的最广泛使用的工具生态系统。Swagger由开源和专业工具组成,满足几乎所有的需求和用例。

一、配置Swagger

添加依赖

// web依赖

        org.springframework.boot
        spring-boot-starter-web


//swagger依赖

    io.springfox
    springfox-swagger2
    2.9.2



    io.springfox
    springfox-swagger-ui
    2.9.2

添加配置类

@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登录密码

添加依赖


    com.github.xiaoymin
    swagger-bootstrap-ui
    1.9.3

更新配置类

@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI     //添加注解
public class SwaggerConfig {
}

添加启动类注解

@EnableSwagger2

配置yaml文件

swagger:
  basic:
    enable: true     // 启用
    username: 用户名
    password: 密码

热门栏目