Skip to content

Commit 8449df9

Browse files
committed
Consider Aligning MvcRequestMatcher's matching methods
Closes gh-9284
1 parent 6499a23 commit 8449df9

File tree

3 files changed

+50
-7
lines changed

3 files changed

+50
-7
lines changed

config/src/test/java/org/springframework/security/config/annotation/web/configurers/AuthorizeHttpRequestsConfigurerTests.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 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.
@@ -314,6 +314,18 @@ public void getWhenServletPathRoleAdminConfiguredAndRoleIsUserThenRespondsWithFo
314314
this.mvc.perform(requestWithUser).andExpect(status().isForbidden());
315315
}
316316

317+
@Test
318+
public void getWhenServletPathRoleAdminConfiguredAndRoleIsUserAndWithoutServletPathThenRespondsWithOk()
319+
throws Exception {
320+
this.spring.register(ServletPathConfig.class, BasicController.class).autowire();
321+
// @formatter:off
322+
MockHttpServletRequestBuilder requestWithUser = get("/")
323+
.with(user("user")
324+
.roles("USER"));
325+
// @formatter:on
326+
this.mvc.perform(requestWithUser).andExpect(status().isOk());
327+
}
328+
317329
@Test
318330
public void getWhenServletPathRoleAdminConfiguredAndRoleIsAdminThenRespondsWithOk() throws Exception {
319331
this.spring.register(ServletPathConfig.class, BasicController.class).autowire();

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2021 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.
@@ -43,6 +43,7 @@
4343
*
4444
* @author Rob Winch
4545
* @author Eddú Meléndez
46+
* @author Evgeniy Cheban
4647
* @since 4.1.1
4748
*/
4849
public class MvcRequestMatcher implements RequestMatcher, RequestVariablesExtractor {
@@ -64,10 +65,7 @@ public MvcRequestMatcher(HandlerMappingIntrospector introspector, String pattern
6465

6566
@Override
6667
public boolean matches(HttpServletRequest request) {
67-
if (this.method != null && !this.method.name().equals(request.getMethod())) {
68-
return false;
69-
}
70-
if (this.servletPath != null && !this.servletPath.equals(request.getServletPath())) {
68+
if (notMatchMethodOrServletPath(request)) {
7169
return false;
7270
}
7371
MatchableHandlerMapping mapping = getMapping(request);
@@ -95,6 +93,9 @@ public Map<String, String> extractUriTemplateVariables(HttpServletRequest reques
9593

9694
@Override
9795
public MatchResult matcher(HttpServletRequest request) {
96+
if (notMatchMethodOrServletPath(request)) {
97+
return MatchResult.notMatch();
98+
}
9899
MatchableHandlerMapping mapping = getMapping(request);
99100
if (mapping == null) {
100101
return this.defaultMatcher.matcher(request);
@@ -103,6 +104,11 @@ public MatchResult matcher(HttpServletRequest request) {
103104
return (result != null) ? MatchResult.match(result.extractUriTemplateVariables()) : MatchResult.notMatch();
104105
}
105106

107+
private boolean notMatchMethodOrServletPath(HttpServletRequest request) {
108+
return this.method != null && !this.method.name().equals(request.getMethod())
109+
|| this.servletPath != null && !this.servletPath.equals(request.getServletPath());
110+
}
111+
106112
/**
107113
* @param method the method to set
108114
*/

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2021 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.
@@ -41,6 +41,7 @@
4141
/**
4242
* @author Rob Winch
4343
* @author Eddú Meléndez
44+
* @author Evgeniy Cheban
4445
*/
4546
@RunWith(MockitoJUnitRunner.class)
4647
public class MvcRequestMatcherTests {
@@ -220,4 +221,28 @@ public void toStringWhenOnlyPattern() {
220221
assertThat(this.matcher.toString()).isEqualTo("Mvc [pattern='/path']");
221222
}
222223

224+
@Test
225+
public void matcherWhenMethodNotMatchesThenNotMatchResult() {
226+
this.matcher.setMethod(HttpMethod.POST);
227+
assertThat(this.matcher.matcher(this.request).isMatch()).isFalse();
228+
}
229+
230+
@Test
231+
public void matcherWhenMethodMatchesThenMatchResult() {
232+
this.matcher.setMethod(HttpMethod.GET);
233+
assertThat(this.matcher.matcher(this.request).isMatch()).isTrue();
234+
}
235+
236+
@Test
237+
public void matcherWhenServletPathNotMatchesThenNotMatchResult() {
238+
this.matcher.setServletPath("/spring");
239+
assertThat(this.matcher.matcher(this.request).isMatch()).isFalse();
240+
}
241+
242+
@Test
243+
public void matcherWhenServletPathMatchesThenMatchResult() {
244+
this.matcher.setServletPath("/path");
245+
assertThat(this.matcher.matcher(this.request).isMatch()).isTrue();
246+
}
247+
223248
}

0 commit comments

Comments
 (0)