本文针对Swagger3 配置后以便我们调用API时包含JWT(JSON WEB TOKEN)
还没添加依赖的请快添加一下
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
自定义ApiKey
private ApiKey apiKey() {
return new ApiKey("Authorization", "Authorization", "header");
}
配置SecurityContext并全局启用
private SecurityContext securityContext() {
return SecurityContext.builder().securityReferences(defaultAuth()).build();
}
private List<SecurityReference> defaultAuth() {
AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
authorizationScopes[0] = authorizationScope;
return Arrays.asList(new SecurityReference("Authorization", authorizationScopes));
}
记得在返回的Docket中启用
return new Docket(DocumentationType.OAS_30)
.enable(true)
.protocols(newHashSet("https", "http"))
.securityContexts(Arrays.asList(securityContext()))
.securitySchemes(Arrays.asList(apiKey()))
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("xxx.xxx.xxx.xxx"))
.build();
效果
配置后所有接口都会自带JWT
这里建议配置swagger-ui 配置教程 使用舒适度提升不止亿点点~~~
有图为证:
|