Support add nested security configurers during builder initialization
Closes gh-17011 Signed-off-by: DingHao <dh.hiekn@gmail.com>
This commit is contained in:
parent
9df3a57d9e
commit
e712ccda9b
|
@ -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.
|
||||
|
@ -50,6 +50,7 @@ import org.springframework.web.filter.DelegatingFilterProxy;
|
|||
* @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>>
|
||||
|
@ -386,10 +387,12 @@ public abstract class AbstractConfiguredSecurityBuilder<O, B extends SecurityBui
|
|||
for (SecurityConfigurer<O, B> configurer : configurers) {
|
||||
configurer.init((B) this);
|
||||
}
|
||||
for (SecurityConfigurer<O, B> configurer : this.configurersAddedInInitializing) {
|
||||
while (!this.configurersAddedInInitializing.isEmpty()) {
|
||||
for (SecurityConfigurer<O, B> configurer : getConfigurersInInitializing()) {
|
||||
configurer.init((B) this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private void configure() throws Exception {
|
||||
|
@ -407,6 +410,12 @@ public abstract class AbstractConfiguredSecurityBuilder<O, B extends SecurityBui
|
|||
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
|
||||
|
|
|
@ -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.
|
||||
|
@ -163,6 +163,34 @@ public class AbstractConfiguredSecurityBuilderTests {
|
|||
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> {
|
||||
|
||||
|
|
Loading…
Reference in New Issue