好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Springboot配置Swagger2登录密码的实现

Swagger

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

一、配置Swagger

添加依赖

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

// 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 >

添加配置类

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

@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();

    }

}

使用

?

1

2

3

4

5

6

7

8

9

10

11

12

13

// 控制层

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

添加依赖

?

1

2

3

4

5

< dependency >

    < groupId >com.github.xiaoymin</ groupId >

    < artifactId >swagger-bootstrap-ui</ artifactId >

    < version >1.9.3</ version >

</ dependency >

更新配置类

?

1

2

3

4

5

@Configuration

@EnableSwagger2

@EnableSwaggerBootstrapUI      //添加注解

public class SwaggerConfig {

}

添加启动类注解

?

1

@EnableSwagger2

配置yaml文件

?

1

2

3

4

5

swagger:

   basic:

    enable: true     // 启用

    username: 用户名

    password: 密码

到此这篇关于Springboot配置Swagger2登录密码的实现的文章就介绍到这了,更多相关Springboot Swagger2登录密码内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://blog.csdn.net/qq_36782325/article/details/120562571

查看更多关于Springboot配置Swagger2登录密码的实现的详细内容...

  阅读:121次