Skip to content

Support add nested security configurers during builder initialization #17020

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -50,6 +50,7 @@
* @param <O> The object that this builder returns
* @param <B> The type of this builder (that is returned by the base class)
* @author Rob Winch
* @author DingHao
* @see WebSecurity
*/
public abstract class AbstractConfiguredSecurityBuilder<O, B extends SecurityBuilder<O>>
Expand Down Expand Up @@ -386,8 +387,10 @@ private void init() throws Exception {
for (SecurityConfigurer<O, B> configurer : configurers) {
configurer.init((B) this);
}
for (SecurityConfigurer<O, B> configurer : this.configurersAddedInInitializing) {
configurer.init((B) this);
while (!this.configurersAddedInInitializing.isEmpty()) {
for (SecurityConfigurer<O, B> configurer : getConfigurersInInitializing()) {
configurer.init((B) this);
}
}
}

Expand All @@ -407,6 +410,12 @@ private Collection<SecurityConfigurer<O, B>> getConfigurers() {
return result;
}

private List<SecurityConfigurer<O, B>> getConfigurersInInitializing() {
List<SecurityConfigurer<O, B>> result = new ArrayList<>(this.configurersAddedInInitializing);
this.configurersAddedInInitializing.clear();
return result;
}

/**
* Determines if the object is unbuilt.
* @return true, if unbuilt else false
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -163,6 +163,34 @@ public void withWhenDuplicateConfigurerAddedThenDuplicateConfigurerRemoved() thr
assertThat(this.builder.getConfigurers(TestSecurityConfigurer.class)).hasSize(1);
}

@Test
public void withWhenConfigurerAddInitializing() throws Exception {
this.builder.with(new FooConfigurer(), Customizer.withDefaults());
assertThat(this.builder.build()).isEqualTo("success");
}

private static class FooConfigurer extends SecurityConfigurerAdapter<Object, TestConfiguredSecurityBuilder> {

@Override
public void init(TestConfiguredSecurityBuilder builder) throws Exception {
builder.with(new BarConfigurer(), Customizer.withDefaults());
}

}

private static class BarConfigurer extends SecurityConfigurerAdapter<Object, TestConfiguredSecurityBuilder> {

@Override
public void init(TestConfiguredSecurityBuilder http) throws Exception {
http.with(new CooConfigurer(), Customizer.withDefaults());
}

}

private static class CooConfigurer extends SecurityConfigurerAdapter<Object, TestConfiguredSecurityBuilder> {

}

private static class ApplyAndRemoveSecurityConfigurer
extends SecurityConfigurerAdapter<Object, TestConfiguredSecurityBuilder> {

Expand Down