swagger2UI之swagger-bootstrap-ui的Authorize功能
⽤Swagger测试接⼝的时候请求头需要携带token参数,swagger-bootstrap-ui就为我们提供了这样的功能。
需要注意的是,swagger-bootstrap-ui在1.9.6版本后更名为knife4j,起始版本仍然为1.9.6,推荐使⽤最新版。
1、背景
(1)Spring Cloud项⽬
(2)依赖版本
<springfox-swagger2.version>2.9.2</springfox-swagger2.version>
<swagger-bootstrap-ui.version>1.9.6</swagger-bootstrap-ui.version>
<knife4j.version>1.9.6</knife4j.version>
<springfox-bean-validators.version>2.9.2</springfox-bean-validators.version>
具体依赖:
a.⽹关处pom添加的依赖:
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>${knife4j.version}</version>
</dependency>
b.⼯程pom添加的依赖:
<!--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>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.6</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-micro-spring-boot-starter</artifactId>
<version>1.9.6</version>
</dependency>神秘的面纱
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-bean-validators</artifactId>
<version>2.9.2</version>
</dependency>
两处依赖请注意knife4j的不同。
2、在⼯程项⽬web包下 XxxWebApplication的同级添加SwaggerConfiguration配置类。
参考的是 和 。
我的仅供参考:
xx;
import com.fasterxml.classmate.TypeResolver;
import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
import com.github.del.OrderExtensions;
import com.github.xiaoymin.swaggerbootstrapui.rvice.SpringAddtionalModelService;茶树精油的功效与作用
llect.Lists;
llect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import t.annotation.Bean;
import t.annotation.Configuration;
import t.annotation.Import;
import annotation.Order;
import springfox.figuration.BeanValidatorPluginsConfiguration; import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.rvice.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.ts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.List;
@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI
浅井江@Import(BeanValidatorPluginsConfiguration.class)
public class SwaggerConfiguration {
@Autowired
SpringAddtionalModelService springAddtionalModelService;
private final TypeResolver typeResolver;
@Autowired
public SwaggerConfiguration(TypeResolver typeResolver) {
}
@Bean(value = "defaultApi")
@Order(value = 1)
public Docket defaultApi() {
ParameterBuilder parameterBuilder = new ParameterBuilder();
List<Parameter> parameters = wArrayList();
parameterBuilder
.name("token")
.description("token令牌")
.modelRef(new ModelRef("String"))
.parameterType("header")
.defaultValue("Bearer")
.required(fal).build();
parameters.add(parameterBuilder.build());
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.
groupName("默认接⼝")
.lect()
.apis(RequestHandlerSelectors.baPackage("xxx"))
//.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) .paths(PathSelectors.any())
.build()
.wArrayList(new OrderExtensions(1)))
.globalOperationParameters(parameters)
.wArrayList(curityContext()))
.curitySchemes(Lists.<SecurityScheme>newArrayList(apiKey()));
}
private ApiKey apiKey() {
return new ApiKey("BearerToken", "Authorization", "header");
return new ApiKey("BearerToken", "Authorization", "header");
}
private SecurityContext curityContext() {
return SecurityContext.builder()
.curityReferences(defaultAuth())
.("/.*"))
.build();
}
List<SecurityReference> defaultAuth() {
AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything"); AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
authorizationScopes[0] = authorizationScope;
wArrayList(new SecurityReference("BearerToken", authorizationScopes));
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("xxxRESTful APIs")
.description("Swagger2 RESTful APIs")
.contact(new Contact("xxx公司", "", ""))
.termsOfServiceUrl("{ip}:{port}/xxx/doc.html")
.version("1.0")
.build();
}
/**
* ----------------------------- 各web的Controller层接⼝ -----------------------------
**/
@Bean(value = "xxApi")
public Docket xxApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.groupName("xx接⼝")
.lect()
.apis(RequestHandlerSelectors.baPackage("web"))
.
paths(PathSelectors.any())
.build()
// .solve(DeveloperApiInfo.class))//name、email、role实体类 .wArrayList(new OrderExtensions(2)))
.wArrayList(curityContext(), curityContext1()))
.curitySchemes(Lists.<SecurityScheme>newArrayList(apiKey1(), apiKey()))
;
}
@Bean(value = "xxxApi")
public Docket xxxApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.groupName("xxx接⼝")
.lect()
.apis(RequestHandlerSelectors.baPackage("web"))
.paths(PathSelectors.any())
.build()
.wArrayList(new OrderExtensions(2)))
.wArrayList(curityContext(), curityContext1()))
.curitySchemes(Lists.<SecurityScheme>newArrayList(apiKey1(), apiKey()))
;
}
@Bean(value = "xxxxApi")
避孕套如何正确使用
public Docket xxxxApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.groupName("xxxx接⼝")
.lect()
.apis(RequestHandlerSelectors.baPackage("web"))
.paths(PathSelectors.any())
.build()
.wArrayList(new OrderExtensions(2)))
.wArrayList(curityContext(), curityContext1()))
.curitySchemes(Lists.<SecurityScheme>newArrayList(apiKey1(), apiKey()))
描写春天的词语有;
}
private ApiKey apiKey1() {
return new ApiKey("BearerToken1", "Authorization-x", "header");
}
private SecurityContext curityContext1() {
return SecurityContext.builder()
.curityReferences(defaultAuth1())
.("/.*"))
.build();
}
List<SecurityReference> defaultAuth1() {
AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything"); AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
authorizationScopes[0] = authorizationScope;
wArrayList(new SecurityReference("BearerToken1", authorizationScopes));
}
}
3、WebApplication加注解:
例如:
@SpringBootApplication
@EnableSwagger2
@EnableSwaggerBootstrapUI
@EnableFeignClients("")
public class XxxWebApplication {
public static void main(String[] args) {
SpringApplication.run(XxxWebApplication.class, args);
}
}
接下来还要在Controller层以及相应的实体类添加注解,
Controller层添加注解⽅法参考我的这篇⽂章:
4、项⽬启动后,访问其doc.html链接
就有了Authorize:
撒贝宁资料
官⽅demo效果:
如有问题欢迎交流探讨~