Skip to content

Commit 496579d

Browse files
eddumelendezjzheaux
authored andcommitted
Add match result for servlet requests
Fixes gh-7148
1 parent 2c836a1 commit 496579d

File tree

7 files changed

+104
-16
lines changed

7 files changed

+104
-16
lines changed

oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/web/DefaultOAuth2AuthorizationRequestResolver.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
*
5151
* @author Joe Grandja
5252
* @author Rob Winch
53+
* @author Eddú Meléndez
5354
* @since 5.1
5455
* @see OAuth2AuthorizationRequestResolver
5556
* @see OAuth2AuthorizationRequestRedirectFilter
@@ -147,7 +148,7 @@ private OAuth2AuthorizationRequest resolve(HttpServletRequest request, String re
147148
private String resolveRegistrationId(HttpServletRequest request) {
148149
if (this.authorizationRequestMatcher.matches(request)) {
149150
return this.authorizationRequestMatcher
150-
.extractUriTemplateVariables(request).get(REGISTRATION_ID_URI_VARIABLE_NAME);
151+
.matcher(request).getVariables().get(REGISTRATION_ID_URI_VARIABLE_NAME);
151152
}
152153
return null;
153154
}

web/src/main/java/org/springframework/security/web/access/expression/ExpressionBasedFilterInvocationSecurityMetadataSource.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -40,6 +40,7 @@
4040
* Expression-based {@code FilterInvocationSecurityMetadataSource}.
4141
*
4242
* @author Luke Taylor
43+
* @author Eddú Meléndez
4344
* @since 3.0
4445
*/
4546
public final class ExpressionBasedFilterInvocationSecurityMetadataSource
@@ -111,7 +112,7 @@ public AntPathMatcherEvaluationContextPostProcessor(
111112

112113
@Override
113114
Map<String, String> extractVariables(HttpServletRequest request) {
114-
return this.matcher.extractUriTemplateVariables(request);
115+
return this.matcher.matcher(request).getVariables();
115116
}
116117
}
117118

web/src/main/java/org/springframework/security/web/servlet/util/matcher/MvcRequestMatcher.java

+18-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,7 +16,6 @@
1616

1717
package org.springframework.security.web.servlet.util.matcher;
1818

19-
import java.util.Collections;
2019
import java.util.Map;
2120

2221
import javax.servlet.http.HttpServletRequest;
@@ -43,6 +42,7 @@
4342
* </p>
4443
*
4544
* @author Rob Winch
45+
* @author Eddú Meléndez
4646
* @since 4.1.1
4747
*/
4848
public class MvcRequestMatcher implements RequestMatcher, RequestVariablesExtractor {
@@ -93,13 +93,18 @@ private MatchableHandlerMapping getMapping(HttpServletRequest request) {
9393
*/
9494
@Override
9595
public Map<String, String> extractUriTemplateVariables(HttpServletRequest request) {
96+
return matcher(request).getVariables();
97+
}
98+
99+
@Override
100+
public MatchResult matcher(HttpServletRequest request) {
96101
MatchableHandlerMapping mapping = getMapping(request);
97102
if (mapping == null) {
98-
return this.defaultMatcher.extractUriTemplateVariables(request);
103+
return this.defaultMatcher.matcher(request);
99104
}
100105
RequestMatchResult result = mapping.match(request, this.pattern);
101-
return result == null ? Collections.<String, String>emptyMap()
102-
: result.extractUriTemplateVariables();
106+
return result == null ? MatchResult.notMatch()
107+
: MatchResult.match(result.extractUriTemplateVariables());
103108
}
104109

105110
/**
@@ -160,12 +165,18 @@ private boolean matches(String lookupPath) {
160165
@Override
161166
public Map<String, String> extractUriTemplateVariables(
162167
HttpServletRequest request) {
168+
return matcher(request).getVariables();
169+
}
170+
171+
@Override
172+
public MatchResult matcher(HttpServletRequest request) {
163173
String lookupPath = this.pathHelper.getLookupPathForRequest(request);
164174
if (matches(lookupPath)) {
165-
return this.pathMatcher.extractUriTemplateVariables(
175+
Map<String, String> variables = this.pathMatcher.extractUriTemplateVariables(
166176
MvcRequestMatcher.this.pattern, lookupPath);
177+
return MatchResult.match(variables);
167178
}
168-
return Collections.emptyMap();
179+
return MatchResult.notMatch();
169180
}
170181
}
171182
}

web/src/main/java/org/springframework/security/web/util/matcher/AntPathRequestMatcher.java

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -49,6 +49,7 @@
4949
*
5050
* @author Luke Taylor
5151
* @author Rob Winch
52+
* @author Eddú Meléndez
5253
* @since 3.1
5354
*
5455
* @see org.springframework.util.AntPathMatcher
@@ -182,11 +183,16 @@ public boolean matches(HttpServletRequest request) {
182183

183184
@Override
184185
public Map<String, String> extractUriTemplateVariables(HttpServletRequest request) {
186+
return matcher(request).getVariables();
187+
}
188+
189+
@Override
190+
public MatchResult matcher(HttpServletRequest request) {
185191
if (this.matcher == null || !matches(request)) {
186-
return Collections.emptyMap();
192+
return MatchResult.notMatch();
187193
}
188194
String url = getRequestPath(request);
189-
return this.matcher.extractUriTemplateVariables(url);
195+
return MatchResult.match(this.matcher.extractUriTemplateVariables(url));
190196
}
191197

192198
private String getRequestPath(HttpServletRequest request) {

web/src/main/java/org/springframework/security/web/util/matcher/RequestMatcher.java

+62-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,12 +15,16 @@
1515
*/
1616
package org.springframework.security.web.util.matcher;
1717

18+
import java.util.Collections;
19+
import java.util.Map;
20+
1821
import javax.servlet.http.HttpServletRequest;
1922

2023
/**
2124
* Simple strategy to match an <tt>HttpServletRequest</tt>.
2225
*
2326
* @author Luke Taylor
27+
* @author Eddú Meléndez
2428
* @since 3.0.2
2529
*/
2630
public interface RequestMatcher {
@@ -33,4 +37,61 @@ public interface RequestMatcher {
3337
*/
3438
boolean matches(HttpServletRequest request);
3539

40+
/**
41+
* @since 5.2
42+
*/
43+
default MatchResult matcher(HttpServletRequest request) {
44+
boolean match = matches(request);
45+
return new MatchResult(match, Collections.emptyMap());
46+
}
47+
48+
/**
49+
* The result of matching
50+
*/
51+
class MatchResult {
52+
private final boolean match;
53+
private final Map<String, String> variables;
54+
55+
MatchResult(boolean match, Map<String, String> variables) {
56+
this.match = match;
57+
this.variables = variables;
58+
}
59+
60+
public boolean isMatch() {
61+
return this.match;
62+
}
63+
64+
public Map<String, String> getVariables() {
65+
return this.variables;
66+
}
67+
68+
/**
69+
* Creates an instance of {@link MatchResult} that is a match with no variables
70+
*
71+
* @return
72+
*/
73+
public static MatchResult match() {
74+
return new MatchResult(true, Collections.emptyMap());
75+
}
76+
77+
/**
78+
* Creates an instance of {@link MatchResult} that is a match with the specified variables
79+
*
80+
* @param variables
81+
* @return
82+
*/
83+
public static MatchResult match(Map<String, String> variables) {
84+
return new MatchResult(true, variables);
85+
}
86+
87+
/**
88+
* Creates an instance of {@link MatchResult} that is not a match.
89+
*
90+
* @return
91+
*/
92+
public static MatchResult notMatch() {
93+
return new MatchResult(false, Collections.emptyMap());
94+
}
95+
}
96+
3697
}

web/src/main/java/org/springframework/security/web/util/matcher/RequestVariablesExtractor.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,6 +25,7 @@
2525
*
2626
* @author Rob Winch
2727
* @since 4.1.1
28+
* @deprecated
2829
*/
2930
public interface RequestVariablesExtractor {
3031

web/src/test/java/org/springframework/security/web/servlet/util/matcher/MvcRequestMatcherTests.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -40,6 +40,7 @@
4040

4141
/**
4242
* @author Rob Winch
43+
* @author Eddú Meléndez
4344
*/
4445
@RunWith(MockitoJUnitRunner.class)
4546
public class MvcRequestMatcherTests {
@@ -73,6 +74,8 @@ public void extractUriTemplateVariablesSuccess() throws Exception {
7374

7475
assertThat(this.matcher.extractUriTemplateVariables(this.request))
7576
.containsEntry("p", "path");
77+
assertThat(this.matcher.matcher(this.request).getVariables())
78+
.containsEntry("p", "path");
7679
}
7780

7881
@Test
@@ -85,6 +88,7 @@ public void extractUriTemplateVariablesFail() throws Exception {
8588
.thenReturn(this.result);
8689

8790
assertThat(this.matcher.extractUriTemplateVariables(this.request)).isEmpty();
91+
assertThat(this.matcher.matcher(this.request).getVariables()).isEmpty();
8892
}
8993

9094
@Test
@@ -94,6 +98,8 @@ public void extractUriTemplateVariablesDefaultSuccess() throws Exception {
9498

9599
assertThat(this.matcher.extractUriTemplateVariables(this.request))
96100
.containsEntry("p", "path");
101+
assertThat(this.matcher.matcher(this.request).getVariables())
102+
.containsEntry("p", "path");
97103
}
98104

99105
@Test
@@ -102,6 +108,7 @@ public void extractUriTemplateVariablesDefaultFail() throws Exception {
102108
when(this.introspector.getMatchableHandlerMapping(this.request)).thenReturn(null);
103109

104110
assertThat(this.matcher.extractUriTemplateVariables(this.request)).isEmpty();
111+
assertThat(this.matcher.matcher(this.request).getVariables()).isEmpty();
105112
}
106113

107114
@Test

0 commit comments

Comments
 (0)