diff --git a/config/src/test/java/org/springframework/security/config/http/HttpHeadersConfigTests.java b/config/src/test/java/org/springframework/security/config/http/HttpHeadersConfigTests.java index 042d9185c4..16a93e9633 100644 --- a/config/src/test/java/org/springframework/security/config/http/HttpHeadersConfigTests.java +++ b/config/src/test/java/org/springframework/security/config/http/HttpHeadersConfigTests.java @@ -28,14 +28,17 @@ import jakarta.servlet.http.HttpSession; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanCreationException; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.beans.factory.parsing.BeanDefinitionParsingException; import org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException; import org.springframework.security.config.test.SpringTestContext; import org.springframework.security.config.test.SpringTestContextExtension; import org.springframework.security.core.Authentication; import org.springframework.security.web.authentication.session.SessionLimit; +import org.springframework.security.web.header.HeaderWriterFilter; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.ResultMatcher; import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; @@ -150,6 +153,16 @@ public class HttpHeadersConfigTests { // @formatter:on } + @Test + public void requestWhenHeadersEagerlyConfiguredThenHeadersAreWritten() throws Exception { + this.spring.configLocations(this.xml("HeadersEagerlyConfigured")).autowire(); + // @formatter:off + this.mvc.perform(get("/").secure(true)) + .andExpect(status().isOk()) + .andExpect(includesDefaults()); + // @formatter:on + } + @Test public void requestWhenFrameOptionsConfiguredThenIncludesHeader() throws Exception { Map headers = new HashMap(defaultHeaders); @@ -955,6 +968,18 @@ public class HttpHeadersConfigTests { } + public static class EagerHeadersBeanPostProcessor implements BeanPostProcessor { + + @Override + public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { + if (bean instanceof HeaderWriterFilter headerWriterFilter) { + headerWriterFilter.setShouldWriteHeadersEagerly(true); + } + return bean; + } + + } + public static class CustomSessionLimit implements SessionLimit { @Override diff --git a/config/src/test/resources/org/springframework/security/config/http/HttpHeadersConfigTests-HeadersEagerlyConfigured.xml b/config/src/test/resources/org/springframework/security/config/http/HttpHeadersConfigTests-HeadersEagerlyConfigured.xml new file mode 100644 index 0000000000..0cb674e409 --- /dev/null +++ b/config/src/test/resources/org/springframework/security/config/http/HttpHeadersConfigTests-HeadersEagerlyConfigured.xml @@ -0,0 +1,37 @@ + + + + + + + + + + + + + + + +