Skip to content

Commit a5b0304

Browse files
committed
Move AnyRequest Validation
This will make way for other adding other checks Issue gh-15982
1 parent c1f5eb3 commit a5b0304

File tree

2 files changed

+53
-13
lines changed

2 files changed

+53
-13
lines changed

config/src/main/java/org/springframework/security/config/annotation/web/builders/WebSecurity.java

+1-13
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@
6565
import org.springframework.security.web.firewall.ObservationMarkingRequestRejectedHandler;
6666
import org.springframework.security.web.firewall.RequestRejectedHandler;
6767
import org.springframework.security.web.firewall.StrictHttpFirewall;
68-
import org.springframework.security.web.util.matcher.AnyRequestMatcher;
6968
import org.springframework.security.web.util.matcher.RequestMatcher;
7069
import org.springframework.security.web.util.matcher.RequestMatcherEntry;
7170
import org.springframework.util.Assert;
@@ -310,20 +309,8 @@ protected Filter performBuild() throws Exception {
310309
requestMatcherPrivilegeEvaluatorsEntries
311310
.add(getRequestMatcherPrivilegeEvaluatorsEntry(securityFilterChain));
312311
}
313-
DefaultSecurityFilterChain anyRequestFilterChain = null;
314312
for (SecurityBuilder<? extends SecurityFilterChain> securityFilterChainBuilder : this.securityFilterChainBuilders) {
315313
SecurityFilterChain securityFilterChain = securityFilterChainBuilder.build();
316-
if (anyRequestFilterChain != null) {
317-
String message = "A filter chain that matches any request [" + anyRequestFilterChain
318-
+ "] has already been configured, which means that this filter chain [" + securityFilterChain
319-
+ "] 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.";
320-
throw new IllegalArgumentException(message);
321-
}
322-
if (securityFilterChain instanceof DefaultSecurityFilterChain defaultSecurityFilterChain) {
323-
if (defaultSecurityFilterChain.getRequestMatcher() instanceof AnyRequestMatcher) {
324-
anyRequestFilterChain = defaultSecurityFilterChain;
325-
}
326-
}
327314
securityFilterChains.add(securityFilterChain);
328315
requestMatcherPrivilegeEvaluatorsEntries
329316
.add(getRequestMatcherPrivilegeEvaluatorsEntry(securityFilterChain));
@@ -345,6 +332,7 @@ else if (!this.observationRegistry.isNoop()) {
345332
new HttpStatusRequestRejectedHandler());
346333
filterChainProxy.setRequestRejectedHandler(requestRejectedHandler);
347334
}
335+
filterChainProxy.setFilterChainValidator(new WebSecurityFilterChainValidator());
348336
filterChainProxy.setFilterChainDecorator(getFilterChainDecorator());
349337
filterChainProxy.afterPropertiesSet();
350338

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2002-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security.config.annotation.web.builders;
18+
19+
import java.util.List;
20+
21+
import org.springframework.security.web.DefaultSecurityFilterChain;
22+
import org.springframework.security.web.FilterChainProxy;
23+
import org.springframework.security.web.SecurityFilterChain;
24+
import org.springframework.security.web.util.matcher.AnyRequestMatcher;
25+
26+
/**
27+
* A filter chain validator for filter chains built by {@link WebSecurity}
28+
*
29+
* @since 6.5
30+
*/
31+
final class WebSecurityFilterChainValidator implements FilterChainProxy.FilterChainValidator {
32+
33+
@Override
34+
public void validate(FilterChainProxy filterChainProxy) {
35+
List<SecurityFilterChain> chains = filterChainProxy.getFilterChains();
36+
DefaultSecurityFilterChain anyRequestFilterChain = null;
37+
for (SecurityFilterChain chain : chains) {
38+
if (anyRequestFilterChain != null) {
39+
String message = "A filter chain that matches any request [" + anyRequestFilterChain
40+
+ "] has already been configured, which means that this filter chain [" + chain
41+
+ "] 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.";
42+
throw new IllegalArgumentException(message);
43+
}
44+
if (chain instanceof DefaultSecurityFilterChain defaultChain) {
45+
if (defaultChain.getRequestMatcher() instanceof AnyRequestMatcher) {
46+
anyRequestFilterChain = defaultChain;
47+
}
48+
}
49+
}
50+
}
51+
52+
}

0 commit comments

Comments
 (0)