Swagger全局参数Token并分组
程序员文章站
2022-03-16 16:30:09
...
导入依赖
最后一个依赖可以不用导入。
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
</dependency>
<!--另外一个UI页面-->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
</dependency>
编写配置文件
全局参数可以设置header query cookie
package top.ddandang.admin_manage.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;
import java.util.ArrayList;
import java.util.List;
/**
* <p>
* 配置Swagger
* </p>
*
* @author D
* @version 1.0
* @date 2020/6/9 16:30
*/
@Configuration
@EnableSwagger2WebMvc
public class SwaggerConfig {
/**
* 全局controller包路径
*/
private static final String BASE_PACKAGE = "top.ddandang.admin_manage.controller";
/**
* 配置swagger Docket实例 默认分组
*
* @return 返回Docket 对象
*/
@Bean
public Docket docket(Environment environment) {
// 设置swagger在dev环境下有效
Profiles profiles = Profiles.of("dev");
boolean flag = environment.acceptsProfiles(profiles);
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.enable(flag)
.select()
.apis(RequestHandlerSelectors.basePackage(BASE_PACKAGE))
.build();
}
/**
* 不需要传递token的接口
*
* @param environment 用户获取环境
* @return Docket
*/
@Bean
public Docket docketNotToken(Environment environment) {
Profiles profiles = Profiles.of("dev");
boolean flag = environment.acceptsProfiles(profiles);
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.enable(flag)
.select()
.apis(RequestHandlerSelectors.basePackage(BASE_PACKAGE))
.paths(PathSelectors.ant("/admin/no-token/**"))
.build()
.groupName("不需要token接口")
.pathMapping("/");
}
/**
* 需要传递token的接口
*
* @param environment 用户获取环境
* @return Docket
*/
@Bean
public Docket docketNeedToken(Environment environment) {
Profiles profiles = Profiles.of("dev");
boolean flag = environment.acceptsProfiles(profiles);
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.enable(flag)
.globalOperationParameters(getGlobalOperationParameters())
.select()
.apis(RequestHandlerSelectors.basePackage(BASE_PACKAGE))
// 可以自己进行定义过滤的接口
.paths(path -> {
// 过滤掉不需要token验证的接口
return !path.contains("no-token");
})
.build()
.groupName("需要token接口")
.pathMapping("/");
}
/**
* 全局参数 可以设置 header, cookie, body, query
*
* @return List<Parameter>
*/
private List<Parameter> getGlobalOperationParameters() {
List<Parameter> pars = new ArrayList<>();
ParameterBuilder parameterBuilder = new ParameterBuilder();
// header query cookie
parameterBuilder.name("Authorization-Token")
.description("认证token")
.modelRef(new ModelRef("string"))
//header, cookie, body, query
.parameterType("header")
.required(true);
pars.add(parameterBuilder.build());
// 还需要其他参数继续添加即可
return pars;
}
/**
* 配置swagger 信息 apiInfo
*
* @return ApiInfo 配置信息
*/
private ApiInfo apiInfo() {
//作者信息
Contact contact = new Contact("菜鸡小分队", "", "*********@qq.com");
return new ApiInfo("***",
"不会写啊铁子!!!",
"1.0", "urn:tos",
contact, "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0", new ArrayList());
}
}
效果图
推荐阅读
-
SpringBoot添加swagger2接口并添加全局Authorization参数
-
SpringBoot添加swagger2接口文档并添加全局Authorization参数
-
Spring Cloud Gateway网关集成Swagger2.9.2 设置全局token
-
配置Swagger带token访问(两种方式:全局或每个接口单独传)
-
springboot配置swagger2 header头部添加token参数
-
Swagger2 设置全局token
-
swagger2带全局token配置
-
.Net Core Swagger进行全局Token添加
-
Swagger2 非全局、无需重复输入的Head参数(Token)配置
-
swagger 全局token设置