File tree Expand file tree Collapse file tree
main/kotlin/org/springframework/security/config/web/servlet
test/kotlin/org/springframework/security/config/web/servlet Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -40,6 +40,7 @@ class HeadersDsl {
4040 private var contentSecurityPolicy: ((HeadersConfigurer <HttpSecurity >.ContentSecurityPolicyConfig ) -> Unit )? = null
4141 private var referrerPolicy: ((HeadersConfigurer <HttpSecurity >.ReferrerPolicyConfig ) -> Unit )? = null
4242 private var featurePolicyDirectives: String? = null
43+ private var disabled = false
4344
4445 var defaultsDisabled: Boolean? = null
4546
@@ -161,6 +162,15 @@ class HeadersDsl {
161162 this .featurePolicyDirectives = policyDirectives
162163 }
163164
165+ /* *
166+ * Disable all HTTP security headers.
167+ *
168+ * @since 5.4
169+ */
170+ fun disable () {
171+ disabled = true
172+ }
173+
164174 internal fun get (): (HeadersConfigurer <HttpSecurity >) -> Unit {
165175 return { headers ->
166176 defaultsDisabled?.also {
@@ -195,6 +205,9 @@ class HeadersDsl {
195205 featurePolicyDirectives?.also {
196206 headers.featurePolicy(featurePolicyDirectives)
197207 }
208+ if (disabled) {
209+ headers.disable()
210+ }
198211 }
199212 }
200213}
Original file line number Diff line number Diff line change @@ -91,4 +91,31 @@ class HeadersDslTests {
9191 }
9292 }
9393 }
94+
95+ @Test
96+ fun `request when headers disabled then no security headers are in the response` () {
97+ this .spring.register(HeadersDisabledConfig ::class .java).autowire()
98+
99+ this .mockMvc.get(" /" )
100+ .andExpect {
101+ header { doesNotExist(ContentTypeOptionsServerHttpHeadersWriter .X_CONTENT_OPTIONS ) }
102+ header { doesNotExist(XFrameOptionsServerHttpHeadersWriter .X_FRAME_OPTIONS ) }
103+ header { doesNotExist(StrictTransportSecurityServerHttpHeadersWriter .STRICT_TRANSPORT_SECURITY ) }
104+ header { doesNotExist(HttpHeaders .CACHE_CONTROL ) }
105+ header { doesNotExist(HttpHeaders .EXPIRES ) }
106+ header { doesNotExist(HttpHeaders .PRAGMA ) }
107+ header { doesNotExist(XXssProtectionServerHttpHeadersWriter .X_XSS_PROTECTION ) }
108+ }
109+ }
110+
111+ @EnableWebSecurity
112+ open class HeadersDisabledConfig : WebSecurityConfigurerAdapter () {
113+ override fun configure (http : HttpSecurity ) {
114+ http {
115+ headers {
116+ disable()
117+ }
118+ }
119+ }
120+ }
94121}
You can’t perform that action at this time.
0 commit comments