This is an automated email from the ASF dual-hosted git repository.
xiaoyu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shenyu.git
The following commit(s) were added to refs/heads/master by this push:
new 96098bf16 [type:feat] cross add originRegex type (#3785)
96098bf16 is described below
commit 96098bf16a8540bac09c5a9da3709dd8be31e969
Author: yunlongn <[email protected]>
AuthorDate: Wed Aug 3 10:39:31 2022 +0800
[type:feat] cross add originRegex type (#3785)
---
shenyu-bootstrap/src/main/resources/application.yml | 1 +
.../apache/shenyu/common/config/ShenyuConfig.java | 20 ++++++++++++++++++++
.../org/apache/shenyu/web/filter/CrossFilter.java | 6 ++++++
.../apache/shenyu/web/filter/CrossFilterTest.java | 11 +++++++++++
4 files changed, 38 insertions(+)
diff --git a/shenyu-bootstrap/src/main/resources/application.yml
b/shenyu-bootstrap/src/main/resources/application.yml
index 03a55ab57..4420234b6 100644
--- a/shenyu-bootstrap/src/main/resources/application.yml
+++ b/shenyu-bootstrap/src/main/resources/application.yml
@@ -169,6 +169,7 @@ shenyu:
# - c.apache.org
# - d.apache.org
# - http://e.apache.org
+# originRegex: ^http(|s)://(.*\.|)abc.com$
allowedExpose: ""
maxAge: "18000"
allowCredentials: true
diff --git
a/shenyu-common/src/main/java/org/apache/shenyu/common/config/ShenyuConfig.java
b/shenyu-common/src/main/java/org/apache/shenyu/common/config/ShenyuConfig.java
index 435f8c927..e1c18a3b0 100644
---
a/shenyu-common/src/main/java/org/apache/shenyu/common/config/ShenyuConfig.java
+++
b/shenyu-common/src/main/java/org/apache/shenyu/common/config/ShenyuConfig.java
@@ -1148,6 +1148,8 @@ public class ShenyuConfig {
private Set<String> origins;
+ private String originRegex;
+
/**
* Gets the spacer.
*
@@ -1219,6 +1221,24 @@ public class ShenyuConfig {
public void setOrigins(final Set<String> origins) {
this.origins = origins;
}
+
+ /**
+ * Gets the originRegex.
+ *
+ * @return the value of originRegex
+ */
+ public String getOriginRegex() {
+ return originRegex;
+ }
+
+ /**
+ * Sets the originRegex.
+ *
+ * @param originRegex originRegex
+ */
+ public void setOriginRegex(final String originRegex) {
+ this.originRegex = originRegex;
+ }
}
}
diff --git
a/shenyu-web/src/main/java/org/apache/shenyu/web/filter/CrossFilter.java
b/shenyu-web/src/main/java/org/apache/shenyu/web/filter/CrossFilter.java
index c61fea17c..6d147dfe1 100644
--- a/shenyu-web/src/main/java/org/apache/shenyu/web/filter/CrossFilter.java
+++ b/shenyu-web/src/main/java/org/apache/shenyu/web/filter/CrossFilter.java
@@ -37,6 +37,7 @@ import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
+import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@@ -87,6 +88,11 @@ public class CrossFilter implements WebFilter {
})
.collect(Collectors.toSet()));
allowCors = allowedOrigin.contains(origin) ||
allowedOrigin.contains(ALL);
+ // if the origin is not allow check match origin again
+ String originRegex;
+ if (!allowCors && StringUtils.isNotBlank(originRegex =
this.filterConfig.getAllowedOrigin().getOriginRegex())) {
+ allowCors = Pattern.matches(originRegex.trim(), origin);
+ }
}
if (allowCors) {
// "Access-Control-Allow-Origin"
diff --git
a/shenyu-web/src/test/java/org/apache/shenyu/web/filter/CrossFilterTest.java
b/shenyu-web/src/test/java/org/apache/shenyu/web/filter/CrossFilterTest.java
index 3f0907a38..960886867 100644
--- a/shenyu-web/src/test/java/org/apache/shenyu/web/filter/CrossFilterTest.java
+++ b/shenyu-web/src/test/java/org/apache/shenyu/web/filter/CrossFilterTest.java
@@ -18,6 +18,7 @@
package org.apache.shenyu.web.filter;
import org.apache.shenyu.common.config.ShenyuConfig.CrossFilterConfig;
+import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
import org.springframework.mock.web.server.MockServerWebExchange;
@@ -27,6 +28,7 @@ import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
import java.util.HashSet;
+import java.util.regex.Pattern;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -107,4 +109,13 @@ public final class CrossFilterTest {
.expectSubscription()
.verifyComplete();
}
+
+ @Test
+ public void testOriginRegex() {
+ final String regex = "^http(|s)://(.*\\.|)abc.com$";
+ Assertions.assertTrue(Pattern.matches(regex,
"http://console.ada.abc.com"));
+ Assertions.assertTrue(Pattern.matches(regex,
"http://console.abc.com"));
+ Assertions.assertTrue(Pattern.matches(regex, "http://abc.com"));
+ Assertions.assertFalse(Pattern.matches(regex, "http://aabc.com"));
+ }
}