Skip to content

Commit 0baf650

Browse files
committed
Merge branch '5.7.x' into 5.8.x
Closes gh-12686
2 parents 82c86b8 + 000b4bc commit 0baf650

File tree

2 files changed

+54
-3
lines changed

2 files changed

+54
-3
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -3270,7 +3270,12 @@ public HttpSecurity addFilterBefore(Filter filter, Class<? extends Filter> befor
32703270
}
32713271

32723272
private HttpSecurity addFilterAtOffsetOf(Filter filter, int offset, Class<? extends Filter> registeredFilter) {
3273-
int order = this.filterOrders.getOrder(registeredFilter) + offset;
3273+
Integer registeredFilterOrder = this.filterOrders.getOrder(registeredFilter);
3274+
if (registeredFilterOrder == null) {
3275+
throw new IllegalArgumentException(
3276+
"The Filter class " + registeredFilter.getName() + " does not have a registered order");
3277+
}
3278+
int order = registeredFilterOrder + offset;
32743279
this.filters.add(new OrderedFilter(filter, order));
32753280
this.filterOrders.put(filter.getClass(), order);
32763281
return this;

config/src/test/java/org/springframework/security/config/annotation/web/builders/HttpSecurityAddFilterTest.java

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,6 +30,7 @@
3030
import org.junit.jupiter.api.Test;
3131
import org.junit.jupiter.api.extension.ExtendWith;
3232

33+
import org.springframework.beans.factory.UnsatisfiedDependencyException;
3334
import org.springframework.context.annotation.Bean;
3435
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
3536
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@@ -45,12 +46,29 @@
4546
import org.springframework.security.web.header.HeaderWriterFilter;
4647

4748
import static org.assertj.core.api.Assertions.assertThat;
49+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
4850

4951
@ExtendWith(SpringTestContextExtension.class)
5052
public class HttpSecurityAddFilterTest {
5153

5254
public final SpringTestContext spring = new SpringTestContext(this);
5355

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+
5472
@Test
5573
public void addFilterAfterWhenSameFilterDifferentPlacesThenOrderCorrect() {
5674
this.spring.register(MyFilterMultipleAfterConfig.class).autowire();
@@ -209,6 +227,34 @@ SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
209227

210228
}
211229

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+
212258
@EnableWebSecurity
213259
static class MyOtherFilterRelativeToMyFilterBeforeConfig {
214260

0 commit comments

Comments
 (0)