Skip to content

Commit d6228e0

Browse files
committed
Merge branch '5.8.x' into 6.2.x
Closes gh-15196
2 parents 8743186 + cdd6266 commit d6228e0

File tree

5 files changed

+26
-49
lines changed

5 files changed

+26
-49
lines changed

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

+14-39
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 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.
@@ -22,6 +22,7 @@
2222
import java.util.LinkedHashMap;
2323
import java.util.List;
2424
import java.util.Map;
25+
import java.util.concurrent.ConcurrentHashMap;
2526
import java.util.concurrent.atomic.AtomicReference;
2627
import java.util.function.Function;
2728

@@ -203,36 +204,15 @@ public C requestMatchers(HttpMethod method, String... patterns) {
203204
if (servletContext == null) {
204205
return requestMatchers(RequestMatchers.antMatchersAsArray(method, patterns));
205206
}
206-
boolean isProgrammaticApiAvailable = isProgrammaticApiAvailable(servletContext);
207207
List<RequestMatcher> matchers = new ArrayList<>();
208208
for (String pattern : patterns) {
209209
AntPathRequestMatcher ant = new AntPathRequestMatcher(pattern, (method != null) ? method.name() : null);
210210
MvcRequestMatcher mvc = createMvcMatchers(method, pattern).get(0);
211-
if (isProgrammaticApiAvailable) {
212-
matchers.add(resolve(ant, mvc, servletContext));
213-
}
214-
else {
215-
this.logger
216-
.warn("The ServletRegistration API was not available at startup time. This may be due to a misconfiguration; "
217-
+ "if you are using AbstractSecurityWebApplicationInitializer, please double-check the recommendations outlined in "
218-
+ "https://docs.spring.io/spring-security/reference/servlet/configuration/java.html#abstractsecuritywebapplicationinitializer-with-spring-mvc");
219-
matchers.add(new DeferredRequestMatcher((request) -> resolve(ant, mvc, request.getServletContext()),
220-
mvc, ant));
221-
}
211+
matchers.add(new DeferredRequestMatcher((c) -> resolve(ant, mvc, c), mvc, ant));
222212
}
223213
return requestMatchers(matchers.toArray(new RequestMatcher[0]));
224214
}
225215

226-
private static boolean isProgrammaticApiAvailable(ServletContext servletContext) {
227-
try {
228-
servletContext.getServletRegistrations();
229-
return true;
230-
}
231-
catch (UnsupportedOperationException ex) {
232-
return false;
233-
}
234-
}
235-
236216
private RequestMatcher resolve(AntPathRequestMatcher ant, MvcRequestMatcher mvc, ServletContext servletContext) {
237217
Map<String, ? extends ServletRegistration> registrations = mappableServletRegistrations(servletContext);
238218
if (registrations.isEmpty()) {
@@ -474,34 +454,29 @@ static List<RequestMatcher> regexMatchers(String... regexPatterns) {
474454

475455
static class DeferredRequestMatcher implements RequestMatcher {
476456

477-
final Function<HttpServletRequest, RequestMatcher> requestMatcherFactory;
457+
final Function<ServletContext, RequestMatcher> requestMatcherFactory;
478458

479459
final AtomicReference<String> description = new AtomicReference<>();
480460

481-
volatile RequestMatcher requestMatcher;
482-
483-
DeferredRequestMatcher(Function<HttpServletRequest, RequestMatcher> resolver, RequestMatcher... candidates) {
484-
this.requestMatcherFactory = (request) -> {
485-
if (this.requestMatcher == null) {
486-
synchronized (this) {
487-
if (this.requestMatcher == null) {
488-
this.requestMatcher = resolver.apply(request);
489-
}
490-
}
491-
}
492-
return this.requestMatcher;
493-
};
461+
final Map<ServletContext, RequestMatcher> requestMatchers = new ConcurrentHashMap<>();
462+
463+
DeferredRequestMatcher(Function<ServletContext, RequestMatcher> resolver, RequestMatcher... candidates) {
464+
this.requestMatcherFactory = (sc) -> this.requestMatchers.computeIfAbsent(sc, resolver);
494465
this.description.set("Deferred " + Arrays.toString(candidates));
495466
}
496467

468+
RequestMatcher requestMatcher(ServletContext servletContext) {
469+
return this.requestMatcherFactory.apply(servletContext);
470+
}
471+
497472
@Override
498473
public boolean matches(HttpServletRequest request) {
499-
return this.requestMatcherFactory.apply(request).matches(request);
474+
return this.requestMatcherFactory.apply(request.getServletContext()).matches(request);
500475
}
501476

502477
@Override
503478
public MatchResult matcher(HttpServletRequest request) {
504-
return this.requestMatcherFactory.apply(request).matcher(request);
479+
return this.requestMatcherFactory.apply(request.getServletContext()).matcher(request);
505480
}
506481

507482
@Override

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

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 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.
@@ -303,11 +303,13 @@ protected List<RequestMatcher> chainRequestMatchers(List<RequestMatcher> request
303303
return requestMatchers;
304304
}
305305

306-
private static List<RequestMatcher> unwrap(List<RequestMatcher> wrappedMatchers) {
306+
private List<RequestMatcher> unwrap(List<RequestMatcher> wrappedMatchers) {
307307
List<RequestMatcher> requestMatchers = new ArrayList<>();
308308
for (RequestMatcher requestMatcher : wrappedMatchers) {
309-
if (requestMatcher instanceof AbstractRequestMatcherRegistry.DeferredRequestMatcher) {
310-
requestMatchers.add(((DeferredRequestMatcher) requestMatcher).requestMatcher);
309+
if (requestMatcher instanceof DeferredRequestMatcher) {
310+
DeferredRequestMatcher deferred = (DeferredRequestMatcher) requestMatcher;
311+
WebApplicationContext web = (WebApplicationContext) getApplicationContext();
312+
requestMatchers.add(deferred.requestMatcher(web.getServletContext()));
311313
}
312314
else {
313315
requestMatchers.add(requestMatcher);

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2024 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.
@@ -78,7 +78,7 @@ public class AuthorizeRequestsTests {
7878
@BeforeEach
7979
public void setup() {
8080
this.servletContext = spy(MockServletContext.mvc());
81-
this.request = new MockHttpServletRequest("GET", "");
81+
this.request = new MockHttpServletRequest(this.servletContext, "GET", "");
8282
this.request.setMethod("GET");
8383
this.response = new MockHttpServletResponse();
8484
this.chain = new MockFilterChain();

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2024 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.
@@ -75,7 +75,7 @@ public class HttpSecuritySecurityMatchersTests {
7575

7676
@BeforeEach
7777
public void setup() throws Exception {
78-
this.request = new MockHttpServletRequest("GET", "");
78+
this.request = new MockHttpServletRequest(MockServletContext.mvc(), "GET", "");
7979
this.request.setMethod("GET");
8080
this.response = new MockHttpServletResponse();
8181
this.chain = new MockFilterChain();

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2024 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.
@@ -72,7 +72,7 @@ public class UrlAuthorizationConfigurerTests {
7272

7373
@BeforeEach
7474
public void setup() {
75-
this.request = new MockHttpServletRequest("GET", "");
75+
this.request = new MockHttpServletRequest(MockServletContext.mvc(), "GET", "");
7676
this.request.setMethod("GET");
7777
this.response = new MockHttpServletResponse();
7878
this.chain = new MockFilterChain();

0 commit comments

Comments
 (0)