Skip to content

Commit eb5b9e1

Browse files
committed
Add UnreachableFilterChainException
1 parent 4b139fb commit eb5b9e1

File tree

3 files changed

+59
-2
lines changed

3 files changed

+59
-2
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import org.springframework.security.web.FilterChainProxy.FilterChainDecorator;
5151
import org.springframework.security.web.FilterInvocation;
5252
import org.springframework.security.web.SecurityFilterChain;
53+
import org.springframework.security.web.UnreachableFilterChainException;
5354
import org.springframework.security.web.access.AuthorizationManagerWebInvocationPrivilegeEvaluator;
5455
import org.springframework.security.web.access.AuthorizationManagerWebInvocationPrivilegeEvaluator.HttpServletRequestTransformer;
5556
import org.springframework.security.web.access.DefaultWebInvocationPrivilegeEvaluator;
@@ -309,7 +310,7 @@ protected Filter performBuild() throws Exception {
309310
String message = "A filter chain that matches any request [" + anyRequestFilterChain
310311
+ "] has already been configured, which means that this filter chain [" + securityFilterChain
311312
+ "] 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.";
312-
throw new IllegalArgumentException(message);
313+
throw new UnreachableFilterChainException(message, securityFilterChain, anyRequestFilterChain);
313314
}
314315
if (securityFilterChain instanceof DefaultSecurityFilterChain defaultSecurityFilterChain) {
315316
if (defaultSecurityFilterChain.getRequestMatcher() instanceof AnyRequestMatcher) {

config/src/test/java/org/springframework/security/config/annotation/web/configuration/WebSecurityConfigurationTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ public void loadConfigWhenTwoSecurityFilterChainsPresentAndSecondWithAnyRequestT
323323
assertThatExceptionOfType(BeanCreationException.class)
324324
.isThrownBy(() -> this.spring.register(MultipleAnyRequestSecurityFilterChainConfig.class).autowire())
325325
.havingRootCause()
326-
.isExactlyInstanceOf(IllegalArgumentException.class);
326+
.isInstanceOf(IllegalArgumentException.class);
327327
}
328328

329329
private void assertAnotherUserPermission(WebInvocationPrivilegeEvaluator privilegeEvaluator) {
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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.web;
18+
19+
/**
20+
* An exception that describes the configuration error of having placed a more
21+
* narrowly-scoped {@link SecurityFilterChain} behind a more broadly-scoped one.
22+
*
23+
* @author Josh Cummings
24+
* @since 6.4
25+
*/
26+
public class UnreachableFilterChainException extends IllegalArgumentException {
27+
28+
private final SecurityFilterChain blocked;
29+
30+
private final SecurityFilterChain blocker;
31+
32+
public UnreachableFilterChainException(String message, SecurityFilterChain blocked, SecurityFilterChain blocker) {
33+
super(message);
34+
this.blocked = blocked;
35+
this.blocker = blocker;
36+
}
37+
38+
/**
39+
* The {@link SecurityFilterChain} that is unreachable due to being blocked by the
40+
* {@link #blocker}.
41+
* @return the blocked {@link SecurityFilterChain}
42+
*/
43+
public SecurityFilterChain getBlocked() {
44+
return this.blocked;
45+
}
46+
47+
/**
48+
* The {@link SecurityFilterChain} that is blocking the unreachable
49+
* {@link SecurityFilterChain}.
50+
* @return the blocking {@link SecurityFilterChain}
51+
*/
52+
public SecurityFilterChain getBlocker() {
53+
return this.blocker;
54+
}
55+
56+
}

0 commit comments

Comments
 (0)