|
@@ -0,0 +1,101 @@
|
|
|
+package com.txz.cif.configurer;
|
|
|
+
|
|
|
+import io.swagger.v3.oas.models.Components;
|
|
|
+import io.swagger.v3.oas.models.OpenAPI;
|
|
|
+import io.swagger.v3.oas.models.security.SecurityRequirement;
|
|
|
+import io.swagger.v3.oas.models.security.SecurityScheme;
|
|
|
+import io.swagger.v3.oas.models.servers.Server;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.cloud.client.ServiceInstance;
|
|
|
+import org.springframework.cloud.client.discovery.DiscoveryClient;
|
|
|
+import org.springframework.context.annotation.Bean;
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+@Configuration
|
|
|
+public class OpenApiConfig {
|
|
|
+
|
|
|
+
|
|
|
+// @Value("${server.port}")
|
|
|
+// private String mallPort;
|
|
|
+ @Resource
|
|
|
+ private DiscoveryClient discoveryClient;
|
|
|
+ @Bean
|
|
|
+ public OpenAPI serviceOpenAPI(@Value("${spring.application.name}") String serviceName) {
|
|
|
+
|
|
|
+// String localIp;
|
|
|
+//
|
|
|
+// {
|
|
|
+// try {
|
|
|
+// localIp = InetAddress.getLocalHost().getHostAddress();
|
|
|
+// } catch (UnknownHostException e) {
|
|
|
+// throw new RuntimeException(e);
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+// // 拼接 `mall` 服务的访问地址(格式:http://IP:端口)
|
|
|
+// String mallServiceUrl = "http://" + localIp + ":" + mallPort;
|
|
|
+
|
|
|
+
|
|
|
+ // 1. 定义第一个请求头:Custom-Header-1
|
|
|
+ SecurityScheme header1 = new SecurityScheme()
|
|
|
+ .type(SecurityScheme.Type.APIKEY)
|
|
|
+ .name("accesstoken") // 第一个请求头名称
|
|
|
+ .in(SecurityScheme.In.HEADER);
|
|
|
+
|
|
|
+ // 2. 定义第二个请求头:Custom-Header-2
|
|
|
+ SecurityScheme header2 = new SecurityScheme()
|
|
|
+ .type(SecurityScheme.Type.APIKEY)
|
|
|
+ .name("token") // 第二个请求头名称
|
|
|
+ .in(SecurityScheme.In.HEADER);
|
|
|
+
|
|
|
+ SecurityScheme header3 = new SecurityScheme()
|
|
|
+ .type(SecurityScheme.Type.APIKEY)
|
|
|
+ .name("Accept-Language") // 第二个请求头名称
|
|
|
+ .in(SecurityScheme.In.HEADER);
|
|
|
+
|
|
|
+ // 3. 注册两个安全方案到组件
|
|
|
+ Components components = new Components()
|
|
|
+ .addSecuritySchemes("Header1Security", header1) // 第一个方案标识
|
|
|
+ .addSecuritySchemes("Header2Security", header2) // 第二个方案标识
|
|
|
+ .addSecuritySchemes("Header3Security", header3); // 第二个方案标识
|
|
|
+
|
|
|
+ // 4. 定义安全要求,同时应用两个请求头
|
|
|
+ List<SecurityRequirement> securityRequirements = new ArrayList<>();
|
|
|
+ securityRequirements.add(new SecurityRequirement().addList("Header1Security"));
|
|
|
+ securityRequirements.add(new SecurityRequirement().addList("Header2Security"));
|
|
|
+ securityRequirements.add(new SecurityRequirement().addList("Header3Security"));
|
|
|
+
|
|
|
+ List<ServiceInstance> gateway = discoveryClient.getInstances("backstage");
|
|
|
+
|
|
|
+ String gatewayUrl = "http://192.168.0.112:8401/cif";
|
|
|
+ if(!CollectionUtils.isEmpty(gateway)){
|
|
|
+ ServiceInstance serviceInstance = gateway.get(0);
|
|
|
+
|
|
|
+ String serviceId = serviceInstance.getServiceId();
|
|
|
+ String host = serviceInstance.getHost();
|
|
|
+ int port = serviceInstance.getPort();
|
|
|
+ gatewayUrl = "http://"+host+":"+port+"/cif";
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ return new OpenAPI()
|
|
|
+ .openapi("3.0.1")
|
|
|
+ .info(new io.swagger.v3.oas.models.info.Info()
|
|
|
+ .title(serviceName + " API")
|
|
|
+ .version("1.0")
|
|
|
+ .description("Documentation for " + serviceName))
|
|
|
+ .servers(Collections.singletonList(
|
|
|
+ new Server().url(gatewayUrl)
|
|
|
+ )).components(components) // 注册组件
|
|
|
+ .security(securityRequirements); // 全局应用安全要求
|
|
|
+
|
|
|
+ }
|
|
|
+}
|