Skip to content

Commit 80ccb9b

Browse files
committed
Merge branch '6.4.x' into 6.5.x
Closes gh-17580
2 parents 409f845 + 829af96 commit 80ccb9b

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

config/src/main/java/org/springframework/security/config/annotation/AbstractConfiguredSecurityBuilder.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 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.
@@ -50,6 +50,7 @@
5050
* @param <O> The object that this builder returns
5151
* @param <B> The type of this builder (that is returned by the base class)
5252
* @author Rob Winch
53+
* @author DingHao
5354
* @see WebSecurity
5455
*/
5556
public abstract class AbstractConfiguredSecurityBuilder<O, B extends SecurityBuilder<O>>
@@ -59,7 +60,7 @@ public abstract class AbstractConfiguredSecurityBuilder<O, B extends SecurityBui
5960

6061
private final LinkedHashMap<Class<? extends SecurityConfigurer<O, B>>, List<SecurityConfigurer<O, B>>> configurers = new LinkedHashMap<>();
6162

62-
private final List<SecurityConfigurer<O, B>> configurersAddedInInitializing = new ArrayList<>();
63+
private List<SecurityConfigurer<O, B>> configurersAddedInInitializing = new ArrayList<>();
6364

6465
private final Map<Class<?>, Object> sharedObjects = new HashMap<>();
6566

@@ -386,8 +387,12 @@ private void init() throws Exception {
386387
for (SecurityConfigurer<O, B> configurer : configurers) {
387388
configurer.init((B) this);
388389
}
389-
for (SecurityConfigurer<O, B> configurer : this.configurersAddedInInitializing) {
390-
configurer.init((B) this);
390+
while (!this.configurersAddedInInitializing.isEmpty()) {
391+
List<SecurityConfigurer<O, B>> toInit = this.configurersAddedInInitializing;
392+
this.configurersAddedInInitializing = new ArrayList<>();
393+
for (SecurityConfigurer<O, B> configurer : toInit) {
394+
configurer.init((B) this);
395+
}
391396
}
392397
}
393398

config/src/test/java/org/springframework/security/config/annotation/web/AbstractConfiguredSecurityBuilderTests.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2025 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.
@@ -163,6 +163,36 @@ public void withWhenDuplicateConfigurerAddedThenDuplicateConfigurerRemoved() thr
163163
assertThat(this.builder.getConfigurers(TestSecurityConfigurer.class)).hasSize(1);
164164
}
165165

166+
@Test
167+
public void withWhenConfigurerAddInitializing() throws Exception {
168+
this.builder.with(new AppliesNestedConfigurer(), Customizer.withDefaults());
169+
assertThat(this.builder.build()).isEqualTo("success");
170+
}
171+
172+
private static class AppliesNestedConfigurer
173+
extends SecurityConfigurerAdapter<Object, TestConfiguredSecurityBuilder> {
174+
175+
@Override
176+
public void init(TestConfiguredSecurityBuilder builder) throws Exception {
177+
builder.with(new NestedConfigurer(), Customizer.withDefaults());
178+
}
179+
180+
}
181+
182+
private static class NestedConfigurer extends SecurityConfigurerAdapter<Object, TestConfiguredSecurityBuilder> {
183+
184+
@Override
185+
public void init(TestConfiguredSecurityBuilder http) throws Exception {
186+
http.with(new DoubleNestedConfigurer(), Customizer.withDefaults());
187+
}
188+
189+
}
190+
191+
private static class DoubleNestedConfigurer
192+
extends SecurityConfigurerAdapter<Object, TestConfiguredSecurityBuilder> {
193+
194+
}
195+
166196
private static class ApplyAndRemoveSecurityConfigurer
167197
extends SecurityConfigurerAdapter<Object, TestConfiguredSecurityBuilder> {
168198

0 commit comments

Comments
 (0)