Swagger2开启验证,保证生产环境安全

最近接口经常被频繁访问,我估计是线上文档泄露了,所以今天看了看如何对swagger文档添加一个密码访问

pom配置

<!-- Swagger2 -->
<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>
<dependency>
    <groupId>com.github.xiaoymin</groupId>
    <artifactId>swagger-bootstrap-ui</artifactId>
    <version>1.9.2</version>
</dependency>
<!-- END:必要组件 -->

swagger配置种开启EnableSwaggerBootstrapUI

在swagger的配置文件种添加注解@EnableSwaggerBootstrapUI

@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI
public class Swagger2Config {}

在全局配置添加验证方式

不让访问文档

在application.yml或application.properties种添加:

swagger: 
  production: true

效果:
在这里插入图片描述

验证密码后访问

swagger:
  basic:
    enable: true
    username: xxx
    password: xxx

效果:
在这里插入图片描述
登陆后即可访问文档