From b50ce662bd3ae5cbdeb252d56773a4a6f3458672 Mon Sep 17 00:00:00 2001
From: M4Y <576811031@qq.com>
Date: Mon, 14 Sep 2020 01:46:07 +0800
Subject: [PATCH] Solve the URI fetching bug in sentinel-zuul-adapter #1109
(#1605)
Use `getRequestURI` instead of `getServletPath` to get URI of current request(Both in prefix and regex matching).
---
.../sentinel-zuul-adapter/pom.xml | 6 ++
.../api/route/PrefixRoutePathMatcher.java | 10 +++-
.../zuul/api/route/RegexRoutePathMatcher.java | 13 +++--
.../zuul/route/SentinelZuulRouteTest.java | 56 +++++++++++++++++++
4 files changed, 79 insertions(+), 6 deletions(-)
create mode 100644 sentinel-adapter/sentinel-zuul-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/gateway/zuul/route/SentinelZuulRouteTest.java
diff --git a/sentinel-adapter/sentinel-zuul-adapter/pom.xml b/sentinel-adapter/sentinel-zuul-adapter/pom.xml
index ea3b624e..7aa5181d 100755
--- a/sentinel-adapter/sentinel-zuul-adapter/pom.xml
+++ b/sentinel-adapter/sentinel-zuul-adapter/pom.xml
@@ -69,6 +69,12 @@
mockito-core
test
+
+ org.springframework
+ spring-test
+ 4.3.20.RELEASE
+ test
+
\ No newline at end of file
diff --git a/sentinel-adapter/sentinel-zuul-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/gateway/zuul/api/route/PrefixRoutePathMatcher.java b/sentinel-adapter/sentinel-zuul-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/gateway/zuul/api/route/PrefixRoutePathMatcher.java
index 21e636e5..b592a9cf 100644
--- a/sentinel-adapter/sentinel-zuul-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/gateway/zuul/api/route/PrefixRoutePathMatcher.java
+++ b/sentinel-adapter/sentinel-zuul-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/gateway/zuul/api/route/PrefixRoutePathMatcher.java
@@ -17,11 +17,12 @@ package com.alibaba.csp.sentinel.adapter.gateway.zuul.api.route;
import com.alibaba.csp.sentinel.util.AssertUtil;
import com.alibaba.csp.sentinel.util.function.Predicate;
-
import com.netflix.zuul.context.RequestContext;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.PathMatcher;
+import javax.servlet.http.HttpServletRequest;
+
/**
* @author Eric Zhao
* @since 1.6.0
@@ -42,7 +43,12 @@ public class PrefixRoutePathMatcher implements Predicate {
@Override
public boolean test(RequestContext context) {
- String path = context.getRequest().getServletPath();
+ //Solve the problem of prefix matching
+ HttpServletRequest request = context.getRequest();
+ String path = request.getRequestURI();
+ if (path == null) {
+ AssertUtil.assertNotBlank(pattern, "requesturi cannot be blank");
+ }
if (canMatch) {
return pathMatcher.match(pattern, path);
}
diff --git a/sentinel-adapter/sentinel-zuul-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/gateway/zuul/api/route/RegexRoutePathMatcher.java b/sentinel-adapter/sentinel-zuul-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/gateway/zuul/api/route/RegexRoutePathMatcher.java
index daf1310e..4363e4e7 100644
--- a/sentinel-adapter/sentinel-zuul-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/gateway/zuul/api/route/RegexRoutePathMatcher.java
+++ b/sentinel-adapter/sentinel-zuul-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/gateway/zuul/api/route/RegexRoutePathMatcher.java
@@ -15,13 +15,13 @@
*/
package com.alibaba.csp.sentinel.adapter.gateway.zuul.api.route;
-import java.util.regex.Pattern;
-
import com.alibaba.csp.sentinel.util.AssertUtil;
import com.alibaba.csp.sentinel.util.function.Predicate;
-
import com.netflix.zuul.context.RequestContext;
+import javax.servlet.http.HttpServletRequest;
+import java.util.regex.Pattern;
+
/**
* @author Eric Zhao
* @since 1.6.0
@@ -39,7 +39,12 @@ public class RegexRoutePathMatcher implements Predicate {
@Override
public boolean test(RequestContext context) {
- String path = context.getRequest().getServletPath();
+ //Solve the problem of route matching
+ HttpServletRequest request = context.getRequest();
+ String path = request.getRequestURI();
+ if (path == null) {
+ AssertUtil.assertNotBlank(pattern, "requesturi cannot be blank");
+ }
return regex.matcher(path).matches();
}
diff --git a/sentinel-adapter/sentinel-zuul-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/gateway/zuul/route/SentinelZuulRouteTest.java b/sentinel-adapter/sentinel-zuul-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/gateway/zuul/route/SentinelZuulRouteTest.java
new file mode 100644
index 00000000..eb67921a
--- /dev/null
+++ b/sentinel-adapter/sentinel-zuul-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/gateway/zuul/route/SentinelZuulRouteTest.java
@@ -0,0 +1,56 @@
+package com.alibaba.csp.sentinel.adapter.gateway.zuul.route;
+
+import com.alibaba.csp.sentinel.adapter.gateway.zuul.api.route.PrefixRoutePathMatcher;
+import com.alibaba.csp.sentinel.adapter.gateway.zuul.api.route.RegexRoutePathMatcher;
+import com.netflix.zuul.context.RequestContext;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.springframework.mock.web.MockHttpServletRequest;
+
+import static com.alibaba.csp.sentinel.adapter.gateway.zuul.constants.ZuulConstant.SERVICE_ID_KEY;
+
+/**
+ * @author: jiangzian
+ **/
+public class SentinelZuulRouteTest {
+
+ private final String SERVICE_ID = "servicea";
+
+ private final String SERVER_NAME = "www.example.com";
+ private final String REQUEST_URI = "/servicea/test.jsp";
+ private final String QUERY_STRING = "param1=value1¶m";
+
+ private RequestContext requestContext = new RequestContext();
+
+
+ @Before
+ public void setUp() {
+ MockHttpServletRequest request = new MockHttpServletRequest();
+ request.setServerName(SERVER_NAME);
+ request.setRequestURI(REQUEST_URI);
+ request.setQueryString(QUERY_STRING);
+ requestContext.set(SERVICE_ID_KEY, SERVICE_ID);
+ requestContext.setRequest(request);
+ RequestContext.testSetCurrentContext(requestContext);
+ }
+
+ @Test
+ public void testPrefixRoutePathMatche() {
+ PrefixRoutePathMatcher prefixRoutePathMatcher = new PrefixRoutePathMatcher("/servicea/????.jsp");
+ Assert.assertTrue(prefixRoutePathMatcher.test(requestContext));
+
+ prefixRoutePathMatcher = new PrefixRoutePathMatcher("/servicea/????.do");
+ Assert.assertTrue(!prefixRoutePathMatcher.test(requestContext));
+ }
+
+ @Test
+ public void testRegexRoutePathMatcher() {
+ RegexRoutePathMatcher regexRoutePathMatcher = new RegexRoutePathMatcher("/servicea/[a-zA-z]+(\\.jsp)");
+ Assert.assertTrue(regexRoutePathMatcher.test(requestContext));
+
+ regexRoutePathMatcher = new RegexRoutePathMatcher("/serviceb/[a-zA-z]+(\\.jsp)");
+ Assert.assertTrue(!regexRoutePathMatcher.test(requestContext));
+ }
+
+}
\ No newline at end of file