Skip to content

Commit 8827b2e

Browse files
committed
Polish Using Request ServletContext
Issue gh-14418
1 parent 75a3579 commit 8827b2e

File tree

2 files changed

+9
-19
lines changed

2 files changed

+9
-19
lines changed

config/src/main/java/org/springframework/security/config/annotation/web/AbstractRequestMatcherRegistry.java

+4-12
Original file line numberDiff line numberDiff line change
@@ -447,18 +447,12 @@ public boolean matches(HttpServletRequest request) {
447447

448448
static class DispatcherServletRequestMatcher implements RequestMatcher {
449449

450-
private final ServletContext servletContext;
451-
452-
DispatcherServletRequestMatcher(ServletContext servletContext) {
453-
this.servletContext = servletContext;
454-
}
455-
456450
@Override
457451
public boolean matches(HttpServletRequest request) {
458452
String name = request.getHttpServletMapping().getServletName();
459-
ServletRegistration registration = this.servletContext.getServletRegistration(name);
453+
ServletRegistration registration = request.getServletContext().getServletRegistration(name);
460454
Assert.notNull(registration,
461-
() -> computeErrorMessage(this.servletContext.getServletRegistrations().values()));
455+
() -> computeErrorMessage(request.getServletContext().getServletRegistrations().values()));
462456
try {
463457
Class<?> clazz = Class.forName(registration.getClassName());
464458
return DispatcherServlet.class.isAssignableFrom(clazz);
@@ -478,10 +472,8 @@ static class DispatcherServletDelegatingRequestMatcher implements RequestMatcher
478472

479473
private final RequestMatcher dispatcherServlet;
480474

481-
DispatcherServletDelegatingRequestMatcher(AntPathRequestMatcher ant, MvcRequestMatcher mvc,
482-
ServletContext servletContext) {
483-
this(ant, mvc, new OrRequestMatcher(new MockMvcRequestMatcher(),
484-
new DispatcherServletRequestMatcher(servletContext)));
475+
DispatcherServletDelegatingRequestMatcher(AntPathRequestMatcher ant, MvcRequestMatcher mvc) {
476+
this(ant, mvc, new OrRequestMatcher(new MockMvcRequestMatcher(), new DispatcherServletRequestMatcher()));
485477
}
486478

487479
DispatcherServletDelegatingRequestMatcher(AntPathRequestMatcher ant, MvcRequestMatcher mvc,

config/src/test/java/org/springframework/security/config/annotation/web/AbstractRequestMatcherRegistryTests.java

+5-7
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ public void requestMatchersWhenPathBasedNonDispatcherServletThenAllows() {
318318
List<RequestMatcher> requestMatchers = this.matcherRegistry.requestMatchers("/services/*");
319319
assertThat(requestMatchers).hasSize(1);
320320
assertThat(requestMatchers.get(0)).isInstanceOf(DispatcherServletDelegatingRequestMatcher.class);
321-
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/services/endpoint");
321+
MockHttpServletRequest request = new MockHttpServletRequest(servletContext, "GET", "/services/endpoint");
322322
request.setHttpServletMapping(TestMockHttpServletMappings.defaultMapping());
323323
assertThat(requestMatchers.get(0).matcher(request).isMatch()).isTrue();
324324
request.setHttpServletMapping(TestMockHttpServletMappings.path(request, "/services"));
@@ -334,9 +334,8 @@ public void matchesWhenDispatcherServletThenMvc() {
334334
servletContext.addServlet("path", Servlet.class).addMapping("/services/*");
335335
MvcRequestMatcher mvc = mock(MvcRequestMatcher.class);
336336
AntPathRequestMatcher ant = mock(AntPathRequestMatcher.class);
337-
DispatcherServletDelegatingRequestMatcher requestMatcher = new DispatcherServletDelegatingRequestMatcher(ant,
338-
mvc, servletContext);
339-
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/services/endpoint");
337+
RequestMatcher requestMatcher = new DispatcherServletDelegatingRequestMatcher(ant, mvc);
338+
MockHttpServletRequest request = new MockHttpServletRequest(servletContext, "GET", "/services/endpoint");
340339
request.setHttpServletMapping(TestMockHttpServletMappings.defaultMapping());
341340
assertThat(requestMatcher.matches(request)).isFalse();
342341
verify(mvc).matches(request);
@@ -354,9 +353,8 @@ public void matchesWhenNoMappingThenException() {
354353
servletContext.addServlet("path", Servlet.class).addMapping("/services/*");
355354
MvcRequestMatcher mvc = mock(MvcRequestMatcher.class);
356355
AntPathRequestMatcher ant = mock(AntPathRequestMatcher.class);
357-
DispatcherServletDelegatingRequestMatcher requestMatcher = new DispatcherServletDelegatingRequestMatcher(ant,
358-
mvc, servletContext);
359-
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/services/endpoint");
356+
RequestMatcher requestMatcher = new DispatcherServletDelegatingRequestMatcher(ant, mvc);
357+
MockHttpServletRequest request = new MockHttpServletRequest(servletContext, "GET", "/services/endpoint");
360358
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> requestMatcher.matcher(request));
361359
}
362360

0 commit comments

Comments
 (0)