|
29 | 29 | import org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping;
|
30 | 30 | import org.springframework.boot.actuate.properties.ManagementServerProperties;
|
31 | 31 | import org.springframework.boot.actuate.properties.SecurityProperties;
|
| 32 | +import org.springframework.boot.actuate.properties.SecurityProperties.Headers; |
32 | 33 | import org.springframework.boot.actuate.properties.SecurityProperties.User;
|
33 | 34 | import org.springframework.boot.actuate.web.ErrorController;
|
34 | 35 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
|
52 | 53 | import org.springframework.security.config.annotation.web.builders.WebSecurity.IgnoredRequestConfigurer;
|
53 | 54 | import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
54 | 55 | import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
| 56 | +import org.springframework.security.config.annotation.web.configurers.HeadersConfigurer; |
55 | 57 | import org.springframework.security.web.AuthenticationEntryPoint;
|
56 | 58 | import org.springframework.security.web.authentication.www.BasicAuthenticationEntryPoint;
|
| 59 | +import org.springframework.security.web.header.writers.HstsHeaderWriter; |
| 60 | +import org.springframework.security.web.util.AnyRequestMatcher; |
57 | 61 |
|
58 | 62 | /**
|
59 | 63 | * {@link EnableAutoConfiguration Auto-configuration} for security of a web application or
|
@@ -149,11 +153,15 @@ protected void configure(HttpSecurity http) throws Exception {
|
149 | 153 | .and().httpBasic() //
|
150 | 154 | .and().anonymous().disable();
|
151 | 155 | }
|
152 |
| - // Remove this when session creation is disabled by default |
153 |
| - http.csrf().disable(); |
| 156 | + if (!this.security.isEnableCsrf()) { |
| 157 | + http.csrf().disable(); |
| 158 | + } |
154 | 159 | // No cookies for application endpoints by default
|
155 | 160 | http.sessionManagement().sessionCreationPolicy(this.security.getSessions());
|
156 | 161 |
|
| 162 | + SecurityAutoConfiguration.configureHeaders(http.headers(), |
| 163 | + this.security.getHeaders()); |
| 164 | + |
157 | 165 | }
|
158 | 166 |
|
159 | 167 | private String[] getSecureApplicationPaths() {
|
@@ -234,6 +242,9 @@ protected void configure(HttpSecurity http) throws Exception {
|
234 | 242 | http.sessionManagement().sessionCreationPolicy(
|
235 | 243 | this.security.getManagement().getSessions());
|
236 | 244 |
|
| 245 | + SecurityAutoConfiguration.configureHeaders(http.headers(), |
| 246 | + this.security.getHeaders()); |
| 247 | + |
237 | 248 | }
|
238 | 249 |
|
239 | 250 | @Override
|
@@ -299,4 +310,26 @@ public AuthenticationManager authenticationManager() throws Exception {
|
299 | 310 |
|
300 | 311 | }
|
301 | 312 |
|
| 313 | + private static void configureHeaders(HeadersConfigurer<?> configurer, |
| 314 | + SecurityProperties.Headers headers) throws Exception { |
| 315 | + if (headers.getHsts() != Headers.HSTS.none) { |
| 316 | + boolean includeSubdomains = headers.getHsts() == Headers.HSTS.all; |
| 317 | + HstsHeaderWriter writer = new HstsHeaderWriter(includeSubdomains); |
| 318 | + writer.setRequestMatcher(new AnyRequestMatcher()); |
| 319 | + configurer.addHeaderWriter(writer); |
| 320 | + } |
| 321 | + if (headers.isContentType()) { |
| 322 | + configurer.contentTypeOptions(); |
| 323 | + } |
| 324 | + if (headers.isXss()) { |
| 325 | + configurer.xssProtection(); |
| 326 | + } |
| 327 | + if (headers.isCache()) { |
| 328 | + configurer.cacheControl(); |
| 329 | + } |
| 330 | + if (headers.isFrame()) { |
| 331 | + configurer.frameOptions(); |
| 332 | + } |
| 333 | + } |
| 334 | + |
302 | 335 | }
|
0 commit comments