温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Springboot怎么配置Swagger2登录密码

发布时间:2022-03-11 16:27:58 来源:亿速云 阅读:2289 作者:iii 栏目:开发技术

本篇内容介绍了“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登录密码”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI