39
39
import org .springframework .security .web .FilterChainProxy ;
40
40
import org .springframework .security .web .FilterInvocation ;
41
41
import org .springframework .security .web .SecurityFilterChain ;
42
+ import org .springframework .security .web .UnreachableFilterChainException ;
42
43
import org .springframework .security .web .access .ExceptionTranslationFilter ;
43
44
import org .springframework .security .web .access .intercept .AuthorizationFilter ;
44
45
import org .springframework .security .web .access .intercept .FilterInvocationSecurityMetadataSource ;
53
54
import org .springframework .security .web .servletapi .SecurityContextHolderAwareRequestFilter ;
54
55
import org .springframework .security .web .session .SessionManagementFilter ;
55
56
import org .springframework .security .web .util .matcher .AnyRequestMatcher ;
56
- import org .springframework .security .web .util .matcher .RequestMatcher ;
57
57
58
58
public class DefaultFilterChainValidator implements FilterChainProxy .FilterChainValidator {
59
59
@@ -75,25 +75,35 @@ private void checkPathOrder(List<SecurityFilterChain> filterChains) {
75
75
// Check that the universal pattern is listed at the end, if at all
76
76
Iterator <SecurityFilterChain > chains = filterChains .iterator ();
77
77
while (chains .hasNext ()) {
78
- RequestMatcher matcher = ((DefaultSecurityFilterChain ) chains .next ()).getRequestMatcher ();
79
- if (AnyRequestMatcher .INSTANCE .equals (matcher ) && chains .hasNext ()) {
80
- throw new IllegalArgumentException ("A universal match pattern ('/**') is defined "
81
- + " before other patterns in the filter chain, causing them to be ignored. Please check the "
82
- + "ordering in your <security:http> namespace or FilterChainProxy bean configuration" );
78
+ if (chains .next () instanceof DefaultSecurityFilterChain securityFilterChain ) {
79
+ if (AnyRequestMatcher .INSTANCE .equals (securityFilterChain .getRequestMatcher ()) && chains .hasNext ()) {
80
+ throw new UnreachableFilterChainException ("A universal match pattern ('/**') is defined "
81
+ + " before other patterns in the filter chain, causing them to be ignored. Please check the "
82
+ + "ordering in your <security:http> namespace or FilterChainProxy bean configuration" ,
83
+ securityFilterChain , chains .next ());
84
+ }
83
85
}
84
86
}
85
87
}
86
88
87
89
private void checkForDuplicateMatchers (List <SecurityFilterChain > chains ) {
88
- while (chains .size () > 1 ) {
89
- DefaultSecurityFilterChain chain = (DefaultSecurityFilterChain ) chains .remove (0 );
90
- for (SecurityFilterChain test : chains ) {
91
- if (chain .getRequestMatcher ().equals (((DefaultSecurityFilterChain ) test ).getRequestMatcher ())) {
92
- throw new IllegalArgumentException ("The FilterChainProxy contains two filter chains using the"
93
- + " matcher " + chain .getRequestMatcher () + ". If you are using multiple <http> namespace "
94
- + "elements, you must use a 'pattern' attribute to define the request patterns to which they apply." );
90
+ DefaultSecurityFilterChain filterChain = null ;
91
+ for (SecurityFilterChain chain : chains ) {
92
+ if (filterChain != null ) {
93
+ if (chain instanceof DefaultSecurityFilterChain defaultChain ) {
94
+ if (defaultChain .getRequestMatcher ().equals (filterChain .getRequestMatcher ())) {
95
+ throw new UnreachableFilterChainException (
96
+ "The FilterChainProxy contains two filter chains using the" + " matcher "
97
+ + defaultChain .getRequestMatcher ()
98
+ + ". If you are using multiple <http> namespace "
99
+ + "elements, you must use a 'pattern' attribute to define the request patterns to which they apply." ,
100
+ defaultChain , chain );
101
+ }
95
102
}
96
103
}
104
+ if (chain instanceof DefaultSecurityFilterChain defaultChain ) {
105
+ filterChain = defaultChain ;
106
+ }
97
107
}
98
108
}
99
109
0 commit comments