|
| 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.ArrayList; |
| 20 | +import java.util.List; |
| 21 | + |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 24 | +import org.mockito.Mock; |
| 25 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 26 | + |
| 27 | +import org.springframework.security.web.DefaultSecurityFilterChain; |
| 28 | +import org.springframework.security.web.FilterChainProxy; |
| 29 | +import org.springframework.security.web.SecurityFilterChain; |
| 30 | +import org.springframework.security.web.UnreachableFilterChainException; |
| 31 | +import org.springframework.security.web.access.ExceptionTranslationFilter; |
| 32 | +import org.springframework.security.web.access.intercept.FilterSecurityInterceptor; |
| 33 | +import org.springframework.security.web.authentication.AnonymousAuthenticationFilter; |
| 34 | +import org.springframework.security.web.util.matcher.AntPathRequestMatcher; |
| 35 | +import org.springframework.security.web.util.matcher.AnyRequestMatcher; |
| 36 | + |
| 37 | +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
| 38 | + |
| 39 | +/** |
| 40 | + * Tests for {@link WebSecurityFilterChainValidator} |
| 41 | + * |
| 42 | + * @author Max Batischev |
| 43 | + */ |
| 44 | +@ExtendWith(MockitoExtension.class) |
| 45 | +public class WebSecurityFilterChainValidatorTests { |
| 46 | + |
| 47 | + private final WebSecurityFilterChainValidator validator = new WebSecurityFilterChainValidator(); |
| 48 | + |
| 49 | + @Mock |
| 50 | + private AnonymousAuthenticationFilter authenticationFilter; |
| 51 | + |
| 52 | + @Mock |
| 53 | + private ExceptionTranslationFilter exceptionTranslationFilter; |
| 54 | + |
| 55 | + @Mock |
| 56 | + private FilterSecurityInterceptor authorizationInterceptor; |
| 57 | + |
| 58 | + @Test |
| 59 | + void validateWhenAnyRequestMatcherIsPresentThenUnreachableFilterChainException() { |
| 60 | + SecurityFilterChain chain1 = new DefaultSecurityFilterChain(AntPathRequestMatcher.antMatcher("/api"), |
| 61 | + this.authenticationFilter, this.exceptionTranslationFilter, this.authorizationInterceptor); |
| 62 | + SecurityFilterChain chain2 = new DefaultSecurityFilterChain(AnyRequestMatcher.INSTANCE, |
| 63 | + this.authenticationFilter, this.exceptionTranslationFilter, this.authorizationInterceptor); |
| 64 | + List<SecurityFilterChain> chains = new ArrayList<>(); |
| 65 | + chains.add(chain2); |
| 66 | + chains.add(chain1); |
| 67 | + FilterChainProxy proxy = new FilterChainProxy(chains); |
| 68 | + |
| 69 | + assertThatExceptionOfType(UnreachableFilterChainException.class) |
| 70 | + .isThrownBy(() -> this.validator.validate(proxy)); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + void validateWhenSameRequestMatchersArePresentThenUnreachableFilterChainException() { |
| 75 | + SecurityFilterChain chain1 = new DefaultSecurityFilterChain(AntPathRequestMatcher.antMatcher("/api"), |
| 76 | + this.authenticationFilter, this.exceptionTranslationFilter, this.authorizationInterceptor); |
| 77 | + SecurityFilterChain chain2 = new DefaultSecurityFilterChain(AntPathRequestMatcher.antMatcher("/api"), |
| 78 | + this.authenticationFilter, this.exceptionTranslationFilter, this.authorizationInterceptor); |
| 79 | + List<SecurityFilterChain> chains = new ArrayList<>(); |
| 80 | + chains.add(chain2); |
| 81 | + chains.add(chain1); |
| 82 | + FilterChainProxy proxy = new FilterChainProxy(chains); |
| 83 | + |
| 84 | + assertThatExceptionOfType(UnreachableFilterChainException.class) |
| 85 | + .isThrownBy(() -> this.validator.validate(proxy)); |
| 86 | + } |
| 87 | + |
| 88 | +} |
0 commit comments