From 22c90cf6bcebdfb5b65e5374f669e0c401b05231 Mon Sep 17 00:00:00 2001 From: weihongyang <1075331873@qq.com> Date: Tue, 21 Jun 2022 13:40:59 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9Eswagger=E7=9A=84confi?= =?UTF-8?q?g=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/cnbm/config/SwaggerConfig.java | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 ym-gateway/src/main/java/com/cnbm/config/SwaggerConfig.java diff --git a/ym-gateway/src/main/java/com/cnbm/config/SwaggerConfig.java b/ym-gateway/src/main/java/com/cnbm/config/SwaggerConfig.java new file mode 100644 index 0000000..6aeac5a --- /dev/null +++ b/ym-gateway/src/main/java/com/cnbm/config/SwaggerConfig.java @@ -0,0 +1,85 @@ +package com.cnbm.config; + +import com.cnbm.common.constant.Constant; +import com.google.common.collect.Lists; +import io.swagger.annotations.ApiOperation; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import springfox.documentation.builders.ApiInfoBuilder; +import springfox.documentation.builders.PathSelectors; +import springfox.documentation.builders.RequestHandlerSelectors; +import springfox.documentation.oas.annotations.EnableOpenApi; +import springfox.documentation.service.*; +import springfox.documentation.spi.DocumentationType; +import springfox.documentation.spi.service.contexts.SecurityContext; +import springfox.documentation.spring.web.plugins.Docket; + +import java.util.Arrays; +import java.util.List; + +import static com.google.common.collect.Lists.newArrayList; + +/** + * @Author weihongyang + * @Date 2022/6/21 10:56 AM + * @Version 1.0 + */ +@Configuration +@EnableOpenApi +public class SwaggerConfig { + + /** + * 创建API应用 + * apiInfo() 增加API相关信息 + * 通过select()函数返回一个ApiSelectorBuilder实例,用来控制哪些接口暴露给Swagger来展现, + * 本例采用指定扫描的包路径来定义指定要建立API的目录。 + * + * @return + */ + @Bean + public Docket restApi() { + return new Docket(DocumentationType.SWAGGER_2) + .groupName("标准接口") + .apiInfo(apiInfo("Spring Boot中使用Swagger2构建RESTful APIs", "1.0")) + .useDefaultResponseMessages(true) + .forCodeGeneration(false) + .select() + .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) + .paths(PathSelectors.any()) + .build() + .securityContexts(Arrays.asList(securityContext())) + // ApiKey的name需与SecurityReference的reference保持一致 + .securitySchemes(Arrays.asList(new ApiKey("token", "token", "header"))); + } + + /** + * 创建该API的基本信息(这些基本信息会展现在文档页面中) + * 访问地址:http://ip:port/swagger-ui.html + * + * @return + */ + private ApiInfo apiInfo(String title, String version) { + return new ApiInfoBuilder() + .title(title) + .description("ym-pass文档") + .termsOfServiceUrl("https://www.baidu.com/") + .version(version) + .build(); + } + + private SecurityContext securityContext() { + return SecurityContext.builder() + .securityReferences(defaultAuth()) + .build(); + } + + List defaultAuth() { + AuthorizationScope authorizationScope + = new AuthorizationScope("global", "accessEverything"); + AuthorizationScope[] authorizationScopes = new AuthorizationScope[1]; + authorizationScopes[0] = authorizationScope; + return Lists.newArrayList( + new SecurityReference(Constant.TOKEN_HEADER, authorizationScopes)); + } + +}