|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2022 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.
|
|
29 | 29 | import org.junit.jupiter.api.Test;
|
30 | 30 | import org.junit.jupiter.api.extension.ExtendWith;
|
31 | 31 |
|
| 32 | +import org.springframework.beans.factory.UnsatisfiedDependencyException; |
32 | 33 | import org.springframework.context.annotation.Bean;
|
33 | 34 | import org.springframework.context.annotation.Configuration;
|
34 | 35 | import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
|
44 | 45 | import org.springframework.security.web.header.HeaderWriterFilter;
|
45 | 46 |
|
46 | 47 | import static org.assertj.core.api.Assertions.assertThat;
|
| 48 | +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
47 | 49 |
|
48 | 50 | @ExtendWith(SpringTestContextExtension.class)
|
49 | 51 | public class HttpSecurityDeferAddFilterTest {
|
50 | 52 |
|
51 | 53 | public final SpringTestContext spring = new SpringTestContext(this);
|
52 | 54 |
|
| 55 | + @Test |
| 56 | + public void addFilterAfterFilterNotRegisteredYetThenThrowIllegalArgument() { |
| 57 | + assertThatExceptionOfType(UnsatisfiedDependencyException.class) |
| 58 | + .isThrownBy( |
| 59 | + () -> this.spring.register(MyOtherFilterAfterMyFilterNotRegisteredYetConfig.class).autowire()) |
| 60 | + .havingRootCause().isInstanceOf(IllegalArgumentException.class); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void addFilterBeforeFilterNotRegisteredYetThenThrowIllegalArgument() { |
| 65 | + assertThatExceptionOfType(UnsatisfiedDependencyException.class) |
| 66 | + .isThrownBy( |
| 67 | + () -> this.spring.register(MyOtherFilterBeforeMyFilterNotRegisteredYetConfig.class).autowire()) |
| 68 | + .havingRootCause().isInstanceOf(IllegalArgumentException.class); |
| 69 | + } |
| 70 | + |
53 | 71 | @Test
|
54 | 72 | public void addFilterAfterWhenSameFilterDifferentPlacesThenOrderCorrect() {
|
55 | 73 | this.spring.register(MyFilterMultipleAfterConfig.class).autowire();
|
@@ -216,6 +234,34 @@ SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
|
216 | 234 | }
|
217 | 235 |
|
218 | 236 | @Configuration
|
| 237 | + @EnableWebSecurity |
| 238 | + static class MyOtherFilterAfterMyFilterNotRegisteredYetConfig { |
| 239 | + |
| 240 | + @Bean |
| 241 | + SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { |
| 242 | + // @formatter:off |
| 243 | + http |
| 244 | + .addFilterAfter(new MyOtherFilter(), MyFilter.class); |
| 245 | + // @formatter:on |
| 246 | + return http.build(); |
| 247 | + } |
| 248 | + |
| 249 | + } |
| 250 | + |
| 251 | + @EnableWebSecurity |
| 252 | + static class MyOtherFilterBeforeMyFilterNotRegisteredYetConfig { |
| 253 | + |
| 254 | + @Bean |
| 255 | + SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { |
| 256 | + // @formatter:off |
| 257 | + http |
| 258 | + .addFilterBefore(new MyOtherFilter(), MyFilter.class); |
| 259 | + // @formatter:on |
| 260 | + return http.build(); |
| 261 | + } |
| 262 | + |
| 263 | + } |
| 264 | + |
219 | 265 | @EnableWebSecurity
|
220 | 266 | static class MyOtherFilterRelativeToMyFilterBeforeConfig {
|
221 | 267 |
|
|
0 commit comments