|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2021 the original author or authors. |
| 2 | + * Copyright 2002-2023 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.
|
|
30 | 30 | import org.junit.jupiter.api.Test;
|
31 | 31 | import org.junit.jupiter.api.extension.ExtendWith;
|
32 | 32 |
|
| 33 | +import org.springframework.beans.factory.UnsatisfiedDependencyException; |
33 | 34 | import org.springframework.context.annotation.Bean;
|
34 | 35 | import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
35 | 36 | import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
|
45 | 46 | import org.springframework.security.web.header.HeaderWriterFilter;
|
46 | 47 |
|
47 | 48 | import static org.assertj.core.api.Assertions.assertThat;
|
| 49 | +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
48 | 50 |
|
49 | 51 | @ExtendWith(SpringTestContextExtension.class)
|
50 | 52 | public class HttpSecurityAddFilterTest {
|
51 | 53 |
|
52 | 54 | public final SpringTestContext spring = new SpringTestContext(this);
|
53 | 55 |
|
| 56 | + @Test |
| 57 | + public void addFilterAfterFilterNotRegisteredYetThenThrowIllegalArgument() { |
| 58 | + assertThatExceptionOfType(UnsatisfiedDependencyException.class) |
| 59 | + .isThrownBy( |
| 60 | + () -> this.spring.register(MyOtherFilterAfterMyFilterNotRegisteredYetConfig.class).autowire()) |
| 61 | + .havingRootCause().isInstanceOf(IllegalArgumentException.class); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void addFilterBeforeFilterNotRegisteredYetThenThrowIllegalArgument() { |
| 66 | + assertThatExceptionOfType(UnsatisfiedDependencyException.class) |
| 67 | + .isThrownBy( |
| 68 | + () -> this.spring.register(MyOtherFilterBeforeMyFilterNotRegisteredYetConfig.class).autowire()) |
| 69 | + .havingRootCause().isInstanceOf(IllegalArgumentException.class); |
| 70 | + } |
| 71 | + |
54 | 72 | @Test
|
55 | 73 | public void addFilterAfterWhenSameFilterDifferentPlacesThenOrderCorrect() {
|
56 | 74 | this.spring.register(MyFilterMultipleAfterConfig.class).autowire();
|
@@ -209,6 +227,34 @@ SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
|
209 | 227 |
|
210 | 228 | }
|
211 | 229 |
|
| 230 | + @EnableWebSecurity |
| 231 | + static class MyOtherFilterAfterMyFilterNotRegisteredYetConfig { |
| 232 | + |
| 233 | + @Bean |
| 234 | + SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { |
| 235 | + // @formatter:off |
| 236 | + http |
| 237 | + .addFilterAfter(new MyOtherFilter(), MyFilter.class); |
| 238 | + // @formatter:on |
| 239 | + return http.build(); |
| 240 | + } |
| 241 | + |
| 242 | + } |
| 243 | + |
| 244 | + @EnableWebSecurity |
| 245 | + static class MyOtherFilterBeforeMyFilterNotRegisteredYetConfig { |
| 246 | + |
| 247 | + @Bean |
| 248 | + SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { |
| 249 | + // @formatter:off |
| 250 | + http |
| 251 | + .addFilterBefore(new MyOtherFilter(), MyFilter.class); |
| 252 | + // @formatter:on |
| 253 | + return http.build(); |
| 254 | + } |
| 255 | + |
| 256 | + } |
| 257 | + |
212 | 258 | @EnableWebSecurity
|
213 | 259 | static class MyOtherFilterRelativeToMyFilterBeforeConfig {
|
214 | 260 |
|
|
0 commit comments