Skip to content

Commit

Permalink
Check Request-Level Servlet Context First
Browse files Browse the repository at this point in the history
Because test do not always attach a fully-formulated ServletContext
to the mock request, it's beneficial to consult the ServletContext
presented during application startup.

In case the request does have needed material, though, it maybe
be helpful for Spring Security to first consult the request.

Issue spring-projectsgh-14418
  • Loading branch information
jzheaux committed May 31, 2024
1 parent 1cc66fa commit 564095d
Showing 1 changed file with 10 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ static class DispatcherServletDelegatingRequestMatcher implements RequestMatcher
@Override
public boolean matches(HttpServletRequest request) {
String name = request.getHttpServletMapping().getServletName();
ServletRegistration registration = this.servletContext.getServletRegistration(name);
ServletRegistration registration = servletRegistration(request, name);
Assert.notNull(registration, "Failed to find servlet [" + name + "] in the servlet context");
if (isDispatcherServlet(registration)) {
return this.mvc.matches(request);
Expand All @@ -540,14 +540,22 @@ public boolean matches(HttpServletRequest request) {
@Override
public MatchResult matcher(HttpServletRequest request) {
String name = request.getHttpServletMapping().getServletName();
ServletRegistration registration = this.servletContext.getServletRegistration(name);
ServletRegistration registration = servletRegistration(request, name);
Assert.notNull(registration, "Failed to find servlet [" + name + "] in the servlet context");
if (isDispatcherServlet(registration)) {
return this.mvc.matcher(request);
}
return this.ant.matcher(request);
}

private ServletRegistration servletRegistration(HttpServletRequest request, String name) {
ServletRegistration registration = request.getServletContext().getServletRegistration(name);
if (registration != null) {
return registration;
}
return this.servletContext.getServletRegistration(name);
}

private boolean isDispatcherServlet(ServletRegistration registration) {
Class<?> dispatcherServlet = ClassUtils
.resolveClassName("org.springframework.web.servlet.DispatcherServlet", null);
Expand Down

0 comments on commit 564095d

Please sign in to comment.