Skip to content

Commit 9f74c12

Browse files
committed
Add PathPatternRequestMatcher
Closes spring-projectsgh-16429
1 parent 51ce91f commit 9f74c12

File tree

2 files changed

+223
-0
lines changed

2 files changed

+223
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/*
2+
* Copyright 2002-2025 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+
* https://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.servlet.util.matcher;
18+
19+
import java.util.Objects;
20+
21+
import jakarta.servlet.http.HttpServletRequest;
22+
23+
import org.springframework.http.server.PathContainer;
24+
import org.springframework.http.server.RequestPath;
25+
import org.springframework.security.web.util.matcher.RequestMatcher;
26+
import org.springframework.security.web.util.matcher.RequestMatchers;
27+
import org.springframework.web.util.ServletRequestPathUtils;
28+
import org.springframework.web.util.pattern.PathPattern;
29+
import org.springframework.web.util.pattern.PathPatternParser;
30+
31+
/**
32+
* A {@link RequestMatcher} that uses {@link PathPattern}s to match against each
33+
* {@link HttpServletRequest}. The provided path should be relative to the servlet (that
34+
* is, it should exclude any context or servlet path).
35+
*
36+
* <p>
37+
* To also match the servlet, please see {@link RequestMatchers#servlet}
38+
*
39+
* <p>
40+
* Note that the {@link org.springframework.web.servlet.HandlerMapping} that contains the
41+
* related URI patterns must be using the same
42+
* {@link org.springframework.web.util.pattern.PathPatternParser} configured in this
43+
* class.
44+
* </p>
45+
*
46+
* @author Josh Cummings
47+
* @since 6.5
48+
*/
49+
public final class PathPatternRequestMatcher implements RequestMatcher {
50+
51+
private final PathPattern pattern;
52+
53+
/**
54+
* Creates a {@link PathPatternRequestMatcher} that uses the provided {@code pattern}.
55+
* <p>
56+
* The {@code pattern} should be relative to the servlet path
57+
* </p>
58+
* @param pattern the pattern used to match
59+
*/
60+
public PathPatternRequestMatcher(PathPattern pattern) {
61+
this.pattern = pattern;
62+
}
63+
64+
/**
65+
* Creates a {@link PathPatternRequestMatcher} that uses the provided {@code pattern},
66+
* parsing it with {@link PathPatternParser#defaultInstance}.
67+
* <p>
68+
* The {@code pattern} should be relative to the servlet path
69+
* </p>
70+
* @param pattern the pattern used to match
71+
*/
72+
public static PathPatternRequestMatcher pathPattern(String pattern) {
73+
PathPatternParser parser = PathPatternParser.defaultInstance;
74+
PathPattern pathPattern = parser.parse(pattern);
75+
return new PathPatternRequestMatcher(pathPattern);
76+
}
77+
78+
/**
79+
* {@inheritDoc}
80+
*/
81+
@Override
82+
public boolean matches(HttpServletRequest request) {
83+
return matcher(request).isMatch();
84+
}
85+
86+
/**
87+
* {@inheritDoc}
88+
*/
89+
@Override
90+
public MatchResult matcher(HttpServletRequest request) {
91+
PathContainer path = getRequestPath(request).pathWithinApplication();
92+
PathPattern.PathMatchInfo info = this.pattern.matchAndExtract(path);
93+
return (info != null) ? MatchResult.match(info.getUriVariables()) : MatchResult.notMatch();
94+
}
95+
96+
private RequestPath getRequestPath(HttpServletRequest request) {
97+
return ServletRequestPathUtils.parseAndCache(request);
98+
}
99+
100+
/**
101+
* {@inheritDoc}
102+
*/
103+
@Override
104+
public boolean equals(Object o) {
105+
if (!(o instanceof PathPatternRequestMatcher that)) {
106+
return false;
107+
}
108+
return Objects.equals(this.pattern, that.pattern);
109+
}
110+
111+
/**
112+
* {@inheritDoc}
113+
*/
114+
@Override
115+
public int hashCode() {
116+
return Objects.hash(this.pattern);
117+
}
118+
119+
/**
120+
* {@inheritDoc}
121+
*/
122+
@Override
123+
public String toString() {
124+
return "PathPattern [" + this.pattern + "]";
125+
}
126+
127+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright 2002-2025 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+
* https://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.servlet.util.matcher;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.http.HttpMethod;
22+
import org.springframework.mock.web.MockHttpServletRequest;
23+
import org.springframework.security.web.util.matcher.RequestMatcher;
24+
import org.springframework.security.web.util.matcher.RequestMatchers;
25+
import org.springframework.web.util.ServletRequestPathUtils;
26+
27+
import static org.assertj.core.api.Assertions.assertThat;
28+
29+
/**
30+
* Tests for {@link PathPatternRequestMatcher}
31+
*/
32+
public class PathPatternRequestMatcherTests {
33+
34+
@Test
35+
void matcherWhenPatternMatchesRequestThenMatchResult() {
36+
RequestMatcher matcher = PathPatternRequestMatcher.pathPattern("/uri");
37+
assertThat(matcher.matches(request("/uri"))).isTrue();
38+
}
39+
40+
@Test
41+
void matcherWhenPatternContainsPlaceholdersThenMatchResult() {
42+
RequestMatcher matcher = PathPatternRequestMatcher.pathPattern("/uri/{username}");
43+
assertThat(matcher.matcher(request("/uri/bob")).getVariables()).containsEntry("username", "bob");
44+
}
45+
46+
@Test
47+
void matcherWhenOnlyPathInfoMatchesThenMatches() {
48+
RequestMatcher matcher = PathPatternRequestMatcher.pathPattern("/uri");
49+
assertThat(matcher.matches(request("GET", "/mvc/uri", "/mvc"))).isTrue();
50+
}
51+
52+
@Test
53+
void matcherWhenUriContainsServletPathThenNoMatch() {
54+
RequestMatcher matcher = PathPatternRequestMatcher.pathPattern("/mvc/uri");
55+
assertThat(matcher.matches(request("GET", "/mvc/uri", "/mvc"))).isFalse();
56+
}
57+
58+
@Test
59+
void matcherWhenSameMethodThenMatchResult() {
60+
RequestMatcher matcher = RequestMatchers.request().methods(HttpMethod.GET).uris("/uri").matcher();
61+
assertThat(matcher.matches(request("/uri"))).isTrue();
62+
}
63+
64+
@Test
65+
void matcherWhenDifferentPathThenNoMatch() {
66+
RequestMatcher matcher = RequestMatchers.request().methods(HttpMethod.GET).uris("/uri").matcher();
67+
assertThat(matcher.matches(request("GET", "/urj", ""))).isFalse();
68+
}
69+
70+
@Test
71+
void matcherWhenDifferentMethodThenNoMatch() {
72+
RequestMatcher matcher = RequestMatchers.request().methods(HttpMethod.GET).uris("/uri").matcher();
73+
assertThat(matcher.matches(request("POST", "/mvc/uri", "/mvc"))).isFalse();
74+
}
75+
76+
@Test
77+
void matcherWhenNoMethodThenMatches() {
78+
RequestMatcher matcher = PathPatternRequestMatcher.pathPattern("/uri");
79+
assertThat(matcher.matches(request("POST", "/uri", ""))).isTrue();
80+
assertThat(matcher.matches(request("GET", "/uri", ""))).isTrue();
81+
}
82+
83+
MockHttpServletRequest request(String uri) {
84+
MockHttpServletRequest request = new MockHttpServletRequest("GET", uri);
85+
ServletRequestPathUtils.parseAndCache(request);
86+
return request;
87+
}
88+
89+
MockHttpServletRequest request(String method, String uri, String servletPath) {
90+
MockHttpServletRequest request = new MockHttpServletRequest(method, uri);
91+
request.setServletPath(servletPath);
92+
ServletRequestPathUtils.parseAndCache(request);
93+
return request;
94+
}
95+
96+
}

0 commit comments

Comments
 (0)