Swagger2中配置添加全局header
程序员文章站
2022-07-02 14:50:43
...
引入maven依赖
<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 api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
.paths(PathSelectors.any())
.build()
.securitySchemes(securitySchemes())
.securityContexts(securityContexts());
}
private List<SecurityContext> securityContexts() {
List<SecurityContext> securityContextList = new ArrayList<>();
List<SecurityReference> securityReferenceList = new ArrayList<>();
securityReferenceList.add(new SecurityReference("custom-token", scopes()));
securityContextList.add(SecurityContext
.builder()
.securityReferences(securityReferenceList)
.forPaths(PathSelectors.any())
.build()
);
return securityContextList;
}
private AuthorizationScope[] scopes() {
return new AuthorizationScope[]{new AuthorizationScope("global", "accessAnything")};
}
private List<ApiKey> securitySchemes() {
List<ApiKey> apiKeyList = new ArrayList<>();
apiKeyList.add(new ApiKey("custom-token", "custom-token", "header"));
return apiKeyList;
}
}
说明
此配置会在swagger-ui页面上部添加authorize模块,点击:
输入后就会在所有接口请求时在header中添加custom-token:[value]
推荐阅读
-
vue2中使用sass并配置全局的sass样式变量的方法
-
Nginx服务器中为网站或目录添加认证密码的配置详解
-
如何在ASP.NET的web.config配置文件中添加MIME类型
-
CentOS7 修改网卡名称为eth0&在VMWare中添加多网卡配置
-
vue中接口域名配置为全局变量的实现方法
-
win2003 vps IIS6中添加站点并绑定域名的配置方法
-
浅谈在fetch方法中添加header后遇到的预检请求问题
-
Winform中对自定义xml配置文件进行Xml节点的添加与删除
-
IDEA中配置文件模板的添加方法
-
SpringBoot添加swagger2接口并添加全局Authorization参数