|
| 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.authentication.configurers; |
| 18 | + |
| 19 | +import javax.servlet.Filter; |
| 20 | +import javax.servlet.ServletException; |
| 21 | +import java.io.IOException; |
| 22 | +import java.util.LinkedHashMap; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Map; |
| 25 | + |
| 26 | +import org.junit.After; |
| 27 | +import org.junit.Before; |
| 28 | +import org.junit.Test; |
| 29 | + |
| 30 | +import org.springframework.mock.web.MockFilterChain; |
| 31 | +import org.springframework.mock.web.MockHttpServletRequest; |
| 32 | +import org.springframework.mock.web.MockHttpServletResponse; |
| 33 | +import org.springframework.mock.web.MockServletContext; |
| 34 | +import org.springframework.security.config.annotation.ObjectPostProcessor; |
| 35 | +import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
| 36 | +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; |
| 37 | +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; |
| 38 | +import org.springframework.security.web.header.HeaderWriterFilter; |
| 39 | +import org.springframework.web.context.ConfigurableWebApplicationContext; |
| 40 | +import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; |
| 41 | + |
| 42 | +import static org.assertj.core.api.Assertions.assertThat; |
| 43 | + |
| 44 | +/** |
| 45 | + * Tests for {@link HeadersConfigurer}. |
| 46 | + * |
| 47 | + * @author Ankur Pathak |
| 48 | + */ |
| 49 | +public class HeadersConfigurerJavaTests { |
| 50 | + |
| 51 | + private boolean allowCircularReferences = false; |
| 52 | + private MockServletContext servletContext; |
| 53 | + private MockHttpServletRequest request; |
| 54 | + private MockHttpServletResponse response; |
| 55 | + private MockFilterChain chain; |
| 56 | + private ConfigurableWebApplicationContext context; |
| 57 | + |
| 58 | + |
| 59 | + @Before |
| 60 | + public void setUp() { |
| 61 | + this.servletContext = new MockServletContext(); |
| 62 | + this.request = new MockHttpServletRequest(this.servletContext, "GET", ""); |
| 63 | + this.response = new MockHttpServletResponse(); |
| 64 | + this.chain = new MockFilterChain(); |
| 65 | + } |
| 66 | + |
| 67 | + |
| 68 | + @After |
| 69 | + public void cleanup(){ |
| 70 | + if (this.context != null){ |
| 71 | + this.context.close(); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + |
| 76 | + @EnableWebSecurity |
| 77 | + public static class HeadersAtTheBeginningOfRequestConfig extends WebSecurityConfigurerAdapter { |
| 78 | + @Override |
| 79 | + protected void configure(HttpSecurity http) throws Exception { |
| 80 | + http |
| 81 | + .headers() |
| 82 | + .addObjectPostProcessor(new ObjectPostProcessor<HeaderWriterFilter>() { |
| 83 | + @Override |
| 84 | + public HeaderWriterFilter postProcess(HeaderWriterFilter filter) { |
| 85 | + filter.setShouldWriteHeadersEagerly(true); |
| 86 | + return filter; |
| 87 | + } |
| 88 | + }); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void headersWrittenAtBeginningOfRequest() throws IOException, ServletException { |
| 94 | + this.context = loadConfig(HeadersAtTheBeginningOfRequestConfig.class); |
| 95 | + this.request.setSecure(true); |
| 96 | + getSpringSecurityFilterChain().doFilter(this.request, this.response, this.chain); |
| 97 | + assertThat(getResponseHeaders()).containsAllEntriesOf(new LinkedHashMap<String, String>(){{ |
| 98 | + put("X-Content-Type-Options", "nosniff"); |
| 99 | + put("X-Frame-Options", "DENY"); |
| 100 | + put("Strict-Transport-Security", "max-age=31536000 ; includeSubDomains"); |
| 101 | + put("Cache-Control", "no-cache, no-store, max-age=0, must-revalidate"); |
| 102 | + put("Expires", "0"); |
| 103 | + put("Pragma", "no-cache"); |
| 104 | + put("X-XSS-Protection", "1; mode=block"); |
| 105 | + }}); |
| 106 | + } |
| 107 | + |
| 108 | + |
| 109 | + @SuppressWarnings("unchecked") |
| 110 | + private Map<String, String > getResponseHeaders() { |
| 111 | + Map<String, String> headers = new LinkedHashMap<>(); |
| 112 | + this.response.getHeaderNames().forEach(name -> { |
| 113 | + List values = this.response.getHeaderValues(name); |
| 114 | + headers.put(name, String.join(",", values)); |
| 115 | + }); |
| 116 | + return headers; |
| 117 | + } |
| 118 | + |
| 119 | + private ConfigurableWebApplicationContext loadConfig(Class<?>... configs) { |
| 120 | + AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); |
| 121 | + context.register(configs); |
| 122 | + context.setAllowCircularReferences(this.allowCircularReferences); |
| 123 | + context.setServletContext(this.servletContext); |
| 124 | + context.refresh(); |
| 125 | + return context; |
| 126 | + } |
| 127 | + |
| 128 | + private Filter getSpringSecurityFilterChain() { |
| 129 | + return this.context.getBean("springSecurityFilterChain", Filter.class); |
| 130 | + } |
| 131 | +} |
0 commit comments