当前位置:网站首页>swagger3整合oauth2 认证token
swagger3整合oauth2 认证token
2022-06-23 05:37:00 【zetor_major】
引入swagger3
<!--Swagger3 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency>
Swagger3在header中,进行Authorization token的认证,配置文件:
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.OAS_30).pathMapping("/")
// 定义是否开启swagger,false为关闭,可以通过变量控制
.enable(swaggerProperties.getEnable())
// 将api的元信息设置为包含在json ResourceListing响应中。
.apiInfo(apiInfo())
// 接口调试地址
.host(swaggerProperties.getTryHost())
// 选择哪些接口作为swagger的doc发布
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build()
// 支持的通讯协议集合
.protocols(newHashSet("https", "http"))
.securitySchemes(securitySchemes())
.securityContexts(securityContexts());
}
/**
* 认证的安全上下文
*/
private List<SecurityScheme> securitySchemes() {
List<SecurityScheme> securitySchemes = new ArrayList<>();
securitySchemes.add(new ApiKey("Authorization", "Authorization", "header"));
return securitySchemes;
}
/**
* 授权信息全局应用
*/
private List<SecurityContext> securityContexts() {
List<SecurityContext> securityContexts = new ArrayList<>();
securityContexts.add(SecurityContext.builder()
.securityReferences(defaultAuth())
.forPaths(PathSelectors.any()).build());
return securityContexts;
}
private List<SecurityReference> defaultAuth() {
AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
authorizationScopes[0] = authorizationScope;
List<SecurityReference> securityReferences = new ArrayList<>();
securityReferences.add(new SecurityReference("Authorization", authorizationScopes));
return securityReferences;
}
访问swagger3 ,地址:http://127.0.0.1:8080/swagger-ui/index.html,
点击Authorize

输入token:

保存如下

源码地址:
边栏推荐
- 2.17 haas506 2.0 development tutorial system (only versions above 2.2 are supported)
- 设计师需要懂的数据指标与数据分析模型
- Coordinate transformation
- C# 获取DPI和真实分辨率的方式(可以解决一直是96的问题)
- Network architecture from Wan to sd-wan edge devices
- C language obtains second, millisecond, subtle and nanosecond timestamps
- 关于监督
- 什么是客户体验自动化?
- 综合培养学生脑力思维的少儿编程
- 了解学习 JSX 的工作方式
猜你喜欢
百度URL参数之LINK?URL参数加密解密研究(代码实例)

bootstrap如何清除浮动的样式

Index - MySQL

如何查看本机IP

Haas506 2.0 development tutorial - Advanced Component Library -modem SMS (only supports versions above 2.2)

Laravel log channel 分组配置

快速认识 WebAssembly

Gridsearchcv (grid search), a model parameter adjuster in sklearn

C# wpf 附加属性实现界面上定义装饰器

Vs+qt project transferred to QT Creator
随机推荐
杂七杂八的东东
Haas506 2.0 development tutorial - Advanced Component Library -modem SMS (only supports versions above 2.2)
原址 交换
JSON to proto
Sklearn classification in sklearn_ Report & accuracy / recall /f1 value
How to view native IP
Shutter style
MySQL5.6 (5.7-8) 基于shardingsphere5.1.1 Sharding-Proxy模式读写分离
快速认识 WebAssembly
Haas506 2.0 development tutorial - Advanced Component Library -modem Voicecall (only supports versions above 2.2)
C language removes line breaks (or other characters) at the end of strings
How to build a data application system based on overall value for energy enterprises
页面嵌入iframe 点击浏览器后退问题
解析创客教育中的个性化学习进度
json转化为proto
279.完全平方数
C# 获取DPI和真实分辨率的方式(可以解决一直是96的问题)
Kubesphere offline deployment without network environment
Linked Storage
1161 Merging Linked Lists