weiximei 6 年之前
父節點
當前提交
87ebc45283

+ 22
- 5
CODE/smart-community/gateway/pom.xml 查看文件

@@ -50,11 +50,11 @@
50 50
             <scope>test</scope>
51 51
         </dependency>
52 52
 
53
-        <dependency>
54
-            <groupId>io.springfox</groupId>
55
-            <artifactId>springfox-swagger2</artifactId>
56
-            <version>2.7.0</version>
57
-        </dependency>
53
+        <!--<dependency>-->
54
+            <!--<groupId>io.springfox</groupId>-->
55
+            <!--<artifactId>springfox-swagger2</artifactId>-->
56
+            <!--<version>2.7.0</version>-->
57
+        <!--</dependency>-->
58 58
 
59 59
         <dependency>
60 60
             <groupId>io.springfox</groupId>
@@ -181,6 +181,23 @@
181 181
             <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
182 182
         </dependency>
183 183
 
184
+        <dependency>
185
+            <groupId>io.springfox</groupId>
186
+            <artifactId>springfox-swagger-ui</artifactId>
187
+            <version>2.9.2</version>
188
+        </dependency>
189
+        <dependency>
190
+            <groupId>io.springfox</groupId>
191
+            <artifactId>springfox-swagger2</artifactId>
192
+            <version>2.9.2</version>
193
+        </dependency>
194
+
195
+        <dependency>
196
+            <groupId>io.springfox</groupId>
197
+            <artifactId>springfox-swagger2</artifactId>
198
+            <version>2.9.2</version>
199
+        </dependency>
200
+
184 201
     </dependencies>
185 202
 
186 203
     <dependencyManagement>

+ 52
- 0
CODE/smart-community/gateway/src/main/java/com/community/huiju/swagger/GatewaySwaggerProvider.java 查看文件

@@ -0,0 +1,52 @@
1
+package com.community.huiju.swagger;
2
+
3
+import org.springframework.cloud.gateway.config.GatewayProperties;
4
+import org.springframework.cloud.gateway.route.RouteLocator;
5
+import org.springframework.cloud.gateway.support.NameUtils;
6
+import org.springframework.context.annotation.Primary;
7
+import org.springframework.stereotype.Component;
8
+import springfox.documentation.swagger.web.SwaggerResource;
9
+import springfox.documentation.swagger.web.SwaggerResourcesProvider;
10
+
11
+import java.util.ArrayList;
12
+import java.util.List;
13
+
14
+/**
15
+ * @Primary 注解的实例优先于其他实例被注入
16
+ */
17
+@Component
18
+@Primary
19
+public class GatewaySwaggerProvider implements SwaggerResourcesProvider {
20
+    public static final String API_URI = "/v2/api-docs";
21
+    private final RouteLocator routeLocator;
22
+    private final GatewayProperties gatewayProperties;
23
+
24
+    public GatewaySwaggerProvider(RouteLocator routeLocator, GatewayProperties gatewayProperties) {
25
+        this.routeLocator = routeLocator;
26
+        this.gatewayProperties = gatewayProperties;
27
+    }
28
+
29
+    @Override
30
+    public List<SwaggerResource> get() {
31
+        List<SwaggerResource> resources = new ArrayList<>();
32
+        List<String> routes = new ArrayList<>();
33
+        //取出Spring Cloud Gateway中的route
34
+        routeLocator.getRoutes().subscribe(route -> routes.add(route.getId()));
35
+        //结合application.yml中的路由配置,只获取有效的route节点
36
+        gatewayProperties.getRoutes().stream().filter(routeDefinition -> routes.contains(routeDefinition.getId()))
37
+                .forEach(routeDefinition -> routeDefinition.getPredicates().stream()
38
+                        .filter(predicateDefinition -> ("Path").equalsIgnoreCase(predicateDefinition.getName()))
39
+                        .forEach(predicateDefinition -> resources.add(swaggerResource(routeDefinition.getId(),
40
+                                predicateDefinition.getArgs().get(NameUtils.GENERATED_NAME_PREFIX + "0")
41
+                                        .replace("/**", API_URI)))));
42
+        return resources;
43
+    }
44
+
45
+    private SwaggerResource swaggerResource(String name, String location) {
46
+        SwaggerResource swaggerResource = new SwaggerResource();
47
+        swaggerResource.setName(name);
48
+        swaggerResource.setLocation(location);
49
+        swaggerResource.setSwaggerVersion("2.0");
50
+        return swaggerResource;
51
+    }
52
+}

+ 28
- 0
CODE/smart-community/gateway/src/main/java/com/community/huiju/swagger/GwSwaggerHeaderFilter.java 查看文件

@@ -0,0 +1,28 @@
1
+package com.community.huiju.swagger;
2
+
3
+import org.springframework.cloud.gateway.filter.GatewayFilter;
4
+import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
5
+import org.springframework.http.server.reactive.ServerHttpRequest;
6
+import org.springframework.stereotype.Component;
7
+import org.springframework.util.StringUtils;
8
+import org.springframework.web.server.ServerWebExchange;
9
+
10
+@Component
11
+public class GwSwaggerHeaderFilter extends AbstractGatewayFilterFactory {
12
+    private static final String HEADER_NAME = "X-Forwarded-Prefix";
13
+
14
+    @Override
15
+    public GatewayFilter apply(Object config) {
16
+        return (exchange, chain) -> {
17
+            ServerHttpRequest request = exchange.getRequest();
18
+            String path = request.getURI().getPath();
19
+            if (!StringUtils.endsWithIgnoreCase(path, GatewaySwaggerProvider.API_URI)) {
20
+                return chain.filter(exchange);
21
+            }
22
+            String basePath = path.substring(0, path.lastIndexOf(GatewaySwaggerProvider.API_URI));
23
+            ServerHttpRequest newRequest = request.mutate().header(HEADER_NAME, basePath).build();
24
+            ServerWebExchange newExchange = exchange.mutate().request(newRequest).build();
25
+            return chain.filter(newExchange);
26
+        };
27
+    }
28
+}

+ 45
- 0
CODE/smart-community/gateway/src/main/java/com/community/huiju/swagger/SwaggerHandler.java 查看文件

@@ -0,0 +1,45 @@
1
+package com.community.huiju.swagger;
2
+
3
+import org.springframework.beans.factory.annotation.Autowired;
4
+import org.springframework.http.HttpStatus;
5
+import org.springframework.http.ResponseEntity;
6
+import org.springframework.web.bind.annotation.GetMapping;
7
+import org.springframework.web.bind.annotation.RequestMapping;
8
+import org.springframework.web.bind.annotation.RestController;
9
+import reactor.core.publisher.Mono;
10
+import springfox.documentation.swagger.web.*;
11
+
12
+import java.util.Optional;
13
+
14
+@RestController
15
+@RequestMapping("/swagger-resources")
16
+public class SwaggerHandler {
17
+    @Autowired(required = false)
18
+    private SecurityConfiguration securityConfiguration;
19
+    @Autowired(required = false)
20
+    private UiConfiguration uiConfiguration;
21
+    private final SwaggerResourcesProvider swaggerResources;
22
+
23
+    @Autowired
24
+    public SwaggerHandler(SwaggerResourcesProvider swaggerResources) {
25
+        this.swaggerResources = swaggerResources;
26
+    }
27
+
28
+
29
+    @GetMapping("/configuration/security")
30
+    public Mono<ResponseEntity<SecurityConfiguration>> securityConfiguration() {
31
+        return Mono.just(new ResponseEntity<>(
32
+                Optional.ofNullable(securityConfiguration).orElse(SecurityConfigurationBuilder.builder().build()), HttpStatus.OK));
33
+    }
34
+
35
+    @GetMapping("/configuration/ui")
36
+    public Mono<ResponseEntity<UiConfiguration>> uiConfiguration() {
37
+        return Mono.just(new ResponseEntity<>(
38
+                Optional.ofNullable(uiConfiguration).orElse(UiConfigurationBuilder.builder().build()), HttpStatus.OK));
39
+    }
40
+
41
+    @GetMapping("")
42
+    public Mono<ResponseEntity> swaggerResources() {
43
+        return Mono.just((new ResponseEntity<>(swaggerResources.get(), HttpStatus.OK)));
44
+    }
45
+}

+ 4
- 0
CODE/smart-community/gateway/src/main/resources/bootstrap.yml 查看文件

@@ -23,6 +23,10 @@ spring:
23 23
     multipart:
24 24
       max-file-size: 10MB
25 25
       max-request-size: 50MB
26
+  security:
27
+    user:
28
+      name: api
29
+      password: 123456
26 30
 #  datasource:
27 31
 #    driver-class-name: com.mysql.jdbc.Driver
28 32
 #    url: jdbc:mysql://rm-uf6z3z6jq11x653d77o.mysql.rds.aliyuncs.com:3306/smart_community_dev?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true