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>bootstrap项目
<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.service.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.service.*;
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(false).build();
parameters.add(parameterBuilder.build());
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.
groupName("默认接⼝")
.select()
.apis(RequestHandlerSelectors.basePackage("xxx"))
//.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) .paths(PathSelectors.any())
.build()
.wArrayList(new OrderExtensions(1)))
.globalOperationParameters(parameters)
.wArrayList(securityContext()))
.securitySchemes(Lists.<SecurityScheme>newArrayList(apiKey()));
}
private ApiKey apiKey() {
return new ApiKey("BearerToken", "Authorization", "header");
return new ApiKey("BearerToken", "Authorization", "header");
}
private SecurityContext securityContext() {
return SecurityContext.builder()
.securityReferences(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接⼝")
.select()
.apis(RequestHandlerSelectors.basePackage("web"))
.
paths(PathSelectors.any())
.build()
// .solve(DeveloperApiInfo.class))//name、email、role实体类 .wArrayList(new OrderExtensions(2)))
.wArrayList(securityContext(), securityContext1()))
.securitySchemes(Lists.<SecurityScheme>newArrayList(apiKey1(), apiKey()))
;
}
@Bean(value = "xxxApi")
public Docket xxxApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.groupName("xxx接⼝")
.select()
.apis(RequestHandlerSelectors.basePackage("web"))
.paths(PathSelectors.any())
.build()
.wArrayList(new OrderExtensions(2)))
.wArrayList(securityContext(), securityContext1()))
.securitySchemes(Lists.<SecurityScheme>newArrayList(apiKey1(), apiKey()))
;
}
@Bean(value = "xxxxApi")
public Docket xxxxApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.groupName("xxxx接⼝")
.select()
.apis(RequestHandlerSelectors.basePackage("web"))
.paths(PathSelectors.any())
.build()
.wArrayList(new OrderExtensions(2)))
.wArrayList(securityContext(), securityContext1()))
.securitySchemes(Lists.<SecurityScheme>newArrayList(apiKey1(), apiKey()))
;
}
private ApiKey apiKey1() {
return new ApiKey("BearerToken1", "Authorization-x", "header");
}
private SecurityContext securityContext1() {
return SecurityContext.builder()
.securityReferences(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效果:
如有问题欢迎交流探讨~
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论