Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solve the problem of prefix and suffix matching #1109 #1605

Merged
merged 6 commits into from
Sep 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions sentinel-adapter/sentinel-zuul-adapter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.20.RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@

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
Expand All @@ -42,7 +43,12 @@ public PrefixRoutePathMatcher(String pattern) {

@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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -39,7 +39,12 @@ public RegexRoutePathMatcher(String pattern) {

@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();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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&param";

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));
}

}