Skip to content

Commit 4318688

Browse files
committed
Add PathPatternRequestMatcher
Fixes: spring-projectsgh-5596
1 parent e6bd535 commit 4318688

File tree

2 files changed

+143
-0
lines changed

2 files changed

+143
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright 2002-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security.web.util.matcher;
18+
19+
import org.springframework.http.HttpMethod;
20+
import org.springframework.http.server.PathContainer;
21+
import org.springframework.util.Assert;
22+
import org.springframework.web.util.UrlPathHelper;
23+
import org.springframework.web.util.pattern.PathPattern;
24+
import org.springframework.web.util.pattern.PathPatternParser;
25+
26+
import javax.servlet.http.HttpServletRequest;
27+
28+
/**
29+
* A {@link RequestMatcher} that leverages the HTTP method and a {@link PathPattern} for matching. The URL is parsed
30+
* using a {@link UrlPathHelper}
31+
*
32+
* @author Rob Winch
33+
* @since 5.1
34+
*/
35+
public class PathPatternRequestMatcher implements RequestMatcher {
36+
private static final PathPatternParser DEFAULT_PATTERN_PARSER = new PathPatternParser();
37+
38+
private static final UrlPathHelper DEFAULT_URL_PATH_HELPER = new UrlPathHelper();
39+
40+
private UrlPathHelper urlPathHelper = DEFAULT_URL_PATH_HELPER;
41+
private final PathPattern pattern;
42+
private final String method;
43+
44+
public PathPatternRequestMatcher(PathPattern pattern) {
45+
this(pattern, null);
46+
}
47+
48+
public PathPatternRequestMatcher(PathPattern pattern, HttpMethod method) {
49+
Assert.notNull(pattern, "pattern cannot be null");
50+
this.pattern = pattern;
51+
this.method = method.name();
52+
}
53+
54+
public PathPatternRequestMatcher(String pattern, HttpMethod method) {
55+
Assert.notNull(pattern, "pattern cannot be null");
56+
this.pattern = DEFAULT_PATTERN_PARSER.parse(pattern);
57+
this.method = method == null ? null : method.name();
58+
}
59+
60+
public PathPatternRequestMatcher(String pattern) {
61+
this(pattern, null);
62+
}
63+
64+
public void setUrlPathHelper(UrlPathHelper urlPathHelper) {
65+
Assert.notNull(urlPathHelper, "urlPathHelper cannot be null");
66+
this.urlPathHelper = urlPathHelper;
67+
}
68+
69+
@Override
70+
public boolean matches(HttpServletRequest request) {
71+
if(this.method != null && !this.method.equals(request.getMethod())) {
72+
return false;
73+
}
74+
String path = this.urlPathHelper.getPathWithinApplication(request);
75+
PathContainer pathContainer = PathContainer.parsePath(path);
76+
return this.pattern.matches(pathContainer);
77+
}
78+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2002-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security.web.util.matcher;
18+
19+
import org.junit.Before;
20+
import org.junit.Test;
21+
import org.springframework.http.HttpMethod;
22+
import org.springframework.mock.web.MockHttpServletRequest;
23+
24+
import static org.assertj.core.api.Assertions.*;
25+
26+
/**
27+
* @author Rob Winch
28+
* @since 5.1
29+
*/
30+
public class PathPatternRequestMatcherTests {
31+
private PathPatternRequestMatcher matcher;
32+
33+
private MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
34+
35+
@Before
36+
public void setup() {
37+
this.matcher = new PathPatternRequestMatcher("/foo");
38+
}
39+
40+
@Test
41+
public void matchesWhenUriExactThenTrue() {
42+
this.request.setRequestURI("/foo");
43+
assertThat(this.matcher.matches(this.request)).isTrue();
44+
}
45+
46+
@Test
47+
public void matchesWhenUrinotMatchThenTrue() {
48+
this.request.setRequestURI("/bar");
49+
assertThat(this.matcher.matches(this.request)).isFalse();
50+
}
51+
52+
@Test
53+
public void matchesWhenDifferentMethodAndExactThenFalse() {
54+
this.matcher = new PathPatternRequestMatcher("/foo", HttpMethod.POST);
55+
this.request.setRequestURI("/foo");
56+
assertThat(this.matcher.matches(this.request)).isFalse();
57+
}
58+
59+
@Test
60+
public void matchesWhenUriTrailingSlashThenTrue() {
61+
this.request.setRequestURI("/foo/");
62+
assertThat(this.matcher.matches(this.request)).isTrue();
63+
}
64+
65+
}

0 commit comments

Comments
 (0)