Commit 119865e88b862b85ae850726629c96862654ab76
1 parent
f966b555
优化
Showing
4 changed files
with
25 additions
and
13 deletions
gateway-core/src/main/java/com/diligrp/xtrade/core/common/utils/PathUtils.java
... | ... | @@ -91,9 +91,6 @@ public class PathUtils { |
91 | 91 | if (pathPatterns == null){ |
92 | 92 | pathPatterns = new CopyOnWriteArrayList<>(); |
93 | 93 | } |
94 | - if (!CollectionUtils.isEmpty(pathPatterns)){ | |
95 | - pathPatterns.clear(); | |
96 | - } | |
97 | 94 | for (String path : paths) { |
98 | 95 | PathPattern pathPattern = PATH_PATTERN_PARSER.parse(path); |
99 | 96 | pathPatterns.add(pathPattern); | ... | ... |
gateway-core/src/main/java/com/diligrp/xtrade/core/config/property/AuthPathProperties.java
... | ... | @@ -3,6 +3,7 @@ package com.diligrp.xtrade.core.config.property; |
3 | 3 | import com.diligrp.xtrade.core.common.utils.PathUtils; |
4 | 4 | import org.springframework.boot.context.properties.ConfigurationProperties; |
5 | 5 | import org.springframework.stereotype.Component; |
6 | +import org.springframework.util.CollectionUtils; | |
6 | 7 | import org.springframework.web.util.pattern.PathPattern; |
7 | 8 | |
8 | 9 | import java.util.List; |
... | ... | @@ -21,13 +22,16 @@ public class AuthPathProperties { |
21 | 22 | |
22 | 23 | |
23 | 24 | /** |
24 | - * 属性配置刷新的时候会从这里进入 | |
25 | + * 属性配置刷新的时候会从这里进入(不要直接注释掉属性配置,否则无法触发) | |
25 | 26 | * @author miaoguoxin |
26 | 27 | * @date 2020/4/13 |
27 | 28 | */ |
28 | 29 | public void setExcludePaths(String[] excludePaths) { |
29 | 30 | this.excludePaths = excludePaths; |
30 | - this.excludePathPatterns = PathUtils.assemblePath(this.excludePaths,this.excludePathPatterns); | |
31 | + if (!CollectionUtils.isEmpty(this.excludePathPatterns)) { | |
32 | + this.excludePathPatterns.clear(); | |
33 | + } | |
34 | + this.excludePathPatterns = PathUtils.assemblePath(this.excludePaths, this.excludePathPatterns); | |
31 | 35 | } |
32 | 36 | |
33 | 37 | ... | ... |
gateway-core/src/main/java/com/diligrp/xtrade/core/support/ApiMetricsInfo.java
... | ... | @@ -92,9 +92,9 @@ public class ApiMetricsInfo { |
92 | 92 | } |
93 | 93 | } |
94 | 94 | |
95 | - private static String getRequestHeaderJson(HttpHeaders headers){ | |
95 | + private static String getRequestHeaderJson(HttpHeaders headers) { | |
96 | 96 | Map<String, List<String>> headerMap = new LinkedHashMap<>(); |
97 | - headerMap.put(HttpHeaders.CONTENT_TYPE,headers.get(HttpHeaders.CONTENT_TYPE)); | |
97 | + headerMap.put(HttpHeaders.CONTENT_TYPE, headers.get(HttpHeaders.CONTENT_TYPE)); | |
98 | 98 | return JsonUtils.toJsonString(headerMap); |
99 | 99 | } |
100 | 100 | ... | ... |
gateway-core/src/main/java/com/diligrp/xtrade/core/support/dispatch/RequestDispatcher.java
... | ... | @@ -13,6 +13,7 @@ import org.springframework.beans.factory.annotation.Autowired; |
13 | 13 | import org.springframework.cloud.gateway.support.NotFoundException; |
14 | 14 | import org.springframework.context.ApplicationContext; |
15 | 15 | import org.springframework.stereotype.Component; |
16 | +import org.springframework.util.CollectionUtils; | |
16 | 17 | import org.springframework.validation.annotation.Validated; |
17 | 18 | import org.springframework.web.server.ServerWebExchange; |
18 | 19 | import org.springframework.web.server.ServerWebInputException; |
... | ... | @@ -56,12 +57,7 @@ public class RequestDispatcher { |
56 | 57 | */ |
57 | 58 | public Message<Object> executeMethod(String uri, String paramsJson, ServerWebExchange exchange) { |
58 | 59 | try { |
59 | - if (HAS_INIT.compareAndSet(false, true)) { | |
60 | - String[] paths = MAPPING_INFO_MAP.keySet().toArray(new String[0]); | |
61 | - if (paths.length > 0) { | |
62 | - PATH_PATTERNS.addAll(PathUtils.assemblePath(paths, PATH_PATTERNS)); | |
63 | - } | |
64 | - } | |
60 | + this.convertPathPatterns(); | |
65 | 61 | String matchUri = PathUtils.getMatchUri(uri, PATH_PATTERNS); |
66 | 62 | if (Strings.isNullOrEmpty(matchUri)) { |
67 | 63 | throw NotFoundException.create(true, String.format("%s not found", uri)); |
... | ... | @@ -74,6 +70,7 @@ public class RequestDispatcher { |
74 | 70 | } |
75 | 71 | } |
76 | 72 | |
73 | + | |
77 | 74 | /** |
78 | 75 | * 注册mapping信息 |
79 | 76 | * |
... | ... | @@ -132,4 +129,18 @@ public class RequestDispatcher { |
132 | 129 | throw tex.getCause() == null ? tex : tex.getCause(); |
133 | 130 | } |
134 | 131 | } |
132 | + | |
133 | + private void convertPathPatterns() { | |
134 | + if (!HAS_INIT.compareAndSet(false, true)) { | |
135 | + return; | |
136 | + } | |
137 | + String[] paths = MAPPING_INFO_MAP.keySet().toArray(new String[0]); | |
138 | + if (paths.length == 0) { | |
139 | + return; | |
140 | + } | |
141 | + if (!CollectionUtils.isEmpty(PATH_PATTERNS)) { | |
142 | + PATH_PATTERNS.clear(); | |
143 | + } | |
144 | + PATH_PATTERNS.addAll(PathUtils.assemblePath(paths, PATH_PATTERNS)); | |
145 | + } | |
135 | 146 | } | ... | ... |