|
| 1 | +/* |
| 2 | + * Copyright 2002-2019 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.security.config.annotation.web.configurers; |
| 18 | + |
| 19 | +import org.junit.Rule; |
| 20 | +import org.junit.Test; |
| 21 | +import org.springframework.beans.factory.annotation.Autowired; |
| 22 | +import org.springframework.security.config.annotation.ObjectPostProcessor; |
| 23 | +import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
| 24 | +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; |
| 25 | +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; |
| 26 | +import org.springframework.security.config.test.SpringTestRule; |
| 27 | +import org.springframework.security.web.header.HeaderWriterFilter; |
| 28 | +import org.springframework.test.web.servlet.MockMvc; |
| 29 | + |
| 30 | +import static org.springframework.http.HttpHeaders.*; |
| 31 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 32 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header; |
| 33 | + |
| 34 | +/** |
| 35 | + * Tests for {@link HeadersConfigurer}. |
| 36 | + * |
| 37 | + * @author Ankur Pathak |
| 38 | + */ |
| 39 | +public class HeadersConfigurerEagerHeadersTests { |
| 40 | + |
| 41 | + @Rule |
| 42 | + public final SpringTestRule spring = new SpringTestRule(); |
| 43 | + |
| 44 | + @Autowired |
| 45 | + MockMvc mvc; |
| 46 | + |
| 47 | + @EnableWebSecurity |
| 48 | + public static class HeadersAtTheBeginningOfRequestConfig extends WebSecurityConfigurerAdapter { |
| 49 | + @Override |
| 50 | + protected void configure(HttpSecurity http) throws Exception { |
| 51 | + //@ formatter:off |
| 52 | + http |
| 53 | + .headers() |
| 54 | + .addObjectPostProcessor(new ObjectPostProcessor<HeaderWriterFilter>() { |
| 55 | + @Override |
| 56 | + public HeaderWriterFilter postProcess(HeaderWriterFilter filter) { |
| 57 | + filter.setShouldWriteHeadersEagerly(true); |
| 58 | + return filter; |
| 59 | + } |
| 60 | + }); |
| 61 | + //@ formatter:on |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void requestWhenHeadersEagerlyConfiguredThenHeadersAreWritten() throws Exception { |
| 67 | + this.spring.register(HeadersAtTheBeginningOfRequestConfig.class).autowire(); |
| 68 | + |
| 69 | + this.mvc.perform(get("/").secure(true)) |
| 70 | + .andExpect(header().string("X-Content-Type-Options", "nosniff")) |
| 71 | + .andExpect(header().string("X-Frame-Options", "DENY")) |
| 72 | + .andExpect(header().string("Strict-Transport-Security", "max-age=31536000 ; includeSubDomains")) |
| 73 | + .andExpect(header().string(CACHE_CONTROL, "no-cache, no-store, max-age=0, must-revalidate")) |
| 74 | + .andExpect(header().string(EXPIRES, "0")) |
| 75 | + .andExpect(header().string(PRAGMA, "no-cache")) |
| 76 | + .andExpect(header().string("X-XSS-Protection", "1; mode=block")); |
| 77 | + } |
| 78 | +} |
0 commit comments