|
23 | 23 | import java.util.List;
|
24 | 24 | import java.util.Map;
|
25 | 25 | import java.util.concurrent.atomic.AtomicReference;
|
26 |
| -import java.util.function.Supplier; |
| 26 | +import java.util.function.Function; |
27 | 27 |
|
28 | 28 | import javax.servlet.DispatcherType;
|
29 | 29 | import javax.servlet.ServletContext;
|
|
44 | 44 | import org.springframework.security.web.util.matcher.RequestMatcher;
|
45 | 45 | import org.springframework.util.Assert;
|
46 | 46 | import org.springframework.util.ClassUtils;
|
47 |
| -import org.springframework.util.function.SingletonSupplier; |
48 | 47 | import org.springframework.web.context.WebApplicationContext;
|
49 | 48 | import org.springframework.web.servlet.handler.HandlerMappingIntrospector;
|
50 | 49 |
|
@@ -327,7 +326,8 @@ public C requestMatchers(HttpMethod method, String... patterns) {
|
327 | 326 | matchers.add(resolve(ant, mvc, servletContext));
|
328 | 327 | }
|
329 | 328 | else {
|
330 |
| - matchers.add(new DeferredRequestMatcher(() -> resolve(ant, mvc, servletContext), mvc, ant)); |
| 329 | + matchers.add(new DeferredRequestMatcher((request) -> resolve(ant, mvc, request.getServletContext()), |
| 330 | + mvc, ant)); |
331 | 331 | }
|
332 | 332 | }
|
333 | 333 | return requestMatchers(matchers.toArray(new RequestMatcher[0]));
|
@@ -584,27 +584,34 @@ static List<RequestMatcher> regexMatchers(String... regexPatterns) {
|
584 | 584 |
|
585 | 585 | static class DeferredRequestMatcher implements RequestMatcher {
|
586 | 586 |
|
587 |
| - final Supplier<RequestMatcher> requestMatcher; |
| 587 | + final Function<HttpServletRequest, RequestMatcher> requestMatcherFactory; |
588 | 588 |
|
589 | 589 | final AtomicReference<String> description = new AtomicReference<>();
|
590 | 590 |
|
591 |
| - DeferredRequestMatcher(Supplier<RequestMatcher> resolver, RequestMatcher... candidates) { |
592 |
| - this.requestMatcher = SingletonSupplier.of(() -> { |
593 |
| - RequestMatcher matcher = resolver.get(); |
594 |
| - this.description.set(matcher.toString()); |
595 |
| - return matcher; |
596 |
| - }); |
| 591 | + volatile RequestMatcher requestMatcher; |
| 592 | + |
| 593 | + DeferredRequestMatcher(Function<HttpServletRequest, RequestMatcher> resolver, RequestMatcher... candidates) { |
| 594 | + this.requestMatcherFactory = (request) -> { |
| 595 | + if (this.requestMatcher == null) { |
| 596 | + synchronized (this) { |
| 597 | + if (this.requestMatcher == null) { |
| 598 | + this.requestMatcher = resolver.apply(request); |
| 599 | + } |
| 600 | + } |
| 601 | + } |
| 602 | + return this.requestMatcher; |
| 603 | + }; |
597 | 604 | this.description.set("Deferred " + Arrays.toString(candidates));
|
598 | 605 | }
|
599 | 606 |
|
600 | 607 | @Override
|
601 | 608 | public boolean matches(HttpServletRequest request) {
|
602 |
| - return this.requestMatcher.get().matches(request); |
| 609 | + return this.requestMatcherFactory.apply(request).matches(request); |
603 | 610 | }
|
604 | 611 |
|
605 | 612 | @Override
|
606 | 613 | public MatchResult matcher(HttpServletRequest request) {
|
607 |
| - return this.requestMatcher.get().matcher(request); |
| 614 | + return this.requestMatcherFactory.apply(request).matcher(request); |
608 | 615 | }
|
609 | 616 |
|
610 | 617 | @Override
|
|
0 commit comments