|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2022 the original author or authors. |
| 2 | + * Copyright 2002-2024 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
63 | 63 | import org.springframework.security.web.firewall.ObservationMarkingRequestRejectedHandler;
|
64 | 64 | import org.springframework.security.web.firewall.RequestRejectedHandler;
|
65 | 65 | import org.springframework.security.web.firewall.StrictHttpFirewall;
|
| 66 | +import org.springframework.security.web.util.matcher.AnyRequestMatcher; |
66 | 67 | import org.springframework.security.web.util.matcher.RequestMatcher;
|
67 | 68 | import org.springframework.security.web.util.matcher.RequestMatcherEntry;
|
68 | 69 | import org.springframework.util.Assert;
|
@@ -296,8 +297,20 @@ protected Filter performBuild() throws Exception {
|
296 | 297 | requestMatcherPrivilegeEvaluatorsEntries
|
297 | 298 | .add(getRequestMatcherPrivilegeEvaluatorsEntry(securityFilterChain));
|
298 | 299 | }
|
| 300 | + boolean anyRequestConfigured = false; |
| 301 | + RequestMatcher matcher = null; |
299 | 302 | for (SecurityBuilder<? extends SecurityFilterChain> securityFilterChainBuilder : this.securityFilterChainBuilders) {
|
300 | 303 | SecurityFilterChain securityFilterChain = securityFilterChainBuilder.build();
|
| 304 | + Assert.isTrue(!anyRequestConfigured, |
| 305 | + "A filter chain that matches any request has already been configured, which means that this filter chain for [" |
| 306 | + + matcher |
| 307 | + + "] will never get invoked. Please use `HttpSecurity#securityMatcher` to ensure that there is only one filter chain configured for 'any request' and that the 'any request' filter chain is published last."); |
| 308 | + if (securityFilterChain instanceof DefaultSecurityFilterChain defaultSecurityFilterChain) { |
| 309 | + matcher = defaultSecurityFilterChain.getRequestMatcher(); |
| 310 | + if (matcher instanceof AnyRequestMatcher) { |
| 311 | + anyRequestConfigured = true; |
| 312 | + } |
| 313 | + } |
301 | 314 | securityFilterChains.add(securityFilterChain);
|
302 | 315 | requestMatcherPrivilegeEvaluatorsEntries
|
303 | 316 | .add(getRequestMatcherPrivilegeEvaluatorsEntry(securityFilterChain));
|
|
0 commit comments