Skip to content

Commit cedb9fd

Browse files
committed
Merge branch '5.8.x' into 6.0.x
Closes gh-12687
2 parents c4f68d8 + 0baf650 commit cedb9fd

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.
@@ -3051,7 +3051,12 @@ public HttpSecurity addFilterBefore(Filter filter, Class<? extends Filter> befor
30513051
}
30523052

30533053
private HttpSecurity addFilterAtOffsetOf(Filter filter, int offset, Class<? extends Filter> registeredFilter) {
3054-
int order = this.filterOrders.getOrder(registeredFilter) + offset;
3054+
Integer registeredFilterOrder = this.filterOrders.getOrder(registeredFilter);
3055+
if (registeredFilterOrder == null) {
3056+
throw new IllegalArgumentException(
3057+
"The Filter class " + registeredFilter.getName() + " does not have a registered order");
3058+
}
3059+
int order = registeredFilterOrder + offset;
30553060
this.filters.add(new OrderedFilter(filter, order));
30563061
this.filterOrders.put(filter.getClass(), order);
30573062
return this;

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

Lines changed: 47 additions & 1 deletion
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.
@@ -29,6 +29,7 @@
2929
import org.junit.jupiter.api.Test;
3030
import org.junit.jupiter.api.extension.ExtendWith;
3131

32+
import org.springframework.beans.factory.UnsatisfiedDependencyException;
3233
import org.springframework.context.annotation.Bean;
3334
import org.springframework.context.annotation.Configuration;
3435
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
@@ -44,12 +45,29 @@
4445
import org.springframework.security.web.header.HeaderWriterFilter;
4546

4647
import static org.assertj.core.api.Assertions.assertThat;
48+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
4749

4850
@ExtendWith(SpringTestContextExtension.class)
4951
public class HttpSecurityDeferAddFilterTest {
5052

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

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+
5371
@Test
5472
public void addFilterAfterWhenSameFilterDifferentPlacesThenOrderCorrect() {
5573
this.spring.register(MyFilterMultipleAfterConfig.class).autowire();
@@ -216,6 +234,34 @@ SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
216234
}
217235

218236
@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+
219265
@EnableWebSecurity
220266
static class MyOtherFilterRelativeToMyFilterBeforeConfig {
221267

0 commit comments

Comments
 (0)