|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2018 the original author or authors. |
| 2 | + * Copyright 2002-2019 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
23 | 23 |
|
24 | 24 | import javax.servlet.http.HttpServletRequest;
|
25 | 25 |
|
| 26 | +import org.springframework.security.config.Customizer; |
26 | 27 | import org.springframework.security.config.annotation.web.HttpSecurityBuilder;
|
27 | 28 | import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
28 | 29 | import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
29 | 30 | import org.springframework.security.web.header.HeaderWriter;
|
30 | 31 | import org.springframework.security.web.header.HeaderWriterFilter;
|
31 | 32 | import org.springframework.security.web.header.writers.*;
|
32 | 33 | import org.springframework.security.web.header.writers.ReferrerPolicyHeaderWriter.ReferrerPolicy;
|
| 34 | +import org.springframework.security.web.header.writers.XContentTypeOptionsHeaderWriter; |
| 35 | +import org.springframework.security.web.header.writers.XXssProtectionHeaderWriter; |
33 | 36 | import org.springframework.security.web.header.writers.frameoptions.XFrameOptionsHeaderWriter;
|
34 | 37 | import org.springframework.security.web.header.writers.frameoptions.XFrameOptionsHeaderWriter.XFrameOptionsMode;
|
35 | 38 | import org.springframework.security.web.util.matcher.RequestMatcher;
|
@@ -121,6 +124,26 @@ public ContentTypeOptionsConfig contentTypeOptions() {
|
121 | 124 | return contentTypeOptions.enable();
|
122 | 125 | }
|
123 | 126 |
|
| 127 | + /** |
| 128 | + * Configures the {@link XContentTypeOptionsHeaderWriter} which inserts the <a href= |
| 129 | + * "https://msdn.microsoft.com/en-us/library/ie/gg622941(v=vs.85).aspx" |
| 130 | + * >X-Content-Type-Options</a>: |
| 131 | + * |
| 132 | + * <pre> |
| 133 | + * X-Content-Type-Options: nosniff |
| 134 | + * </pre> |
| 135 | + * |
| 136 | + * @param contentTypeOptionsCustomizer the {@link Customizer} to provide more options for |
| 137 | + * the {@link ContentTypeOptionsConfig} |
| 138 | + * @return the {@link HeadersConfigurer} for additional customizations |
| 139 | + * @throws Exception |
| 140 | + */ |
| 141 | + public HeadersConfigurer<H> contentTypeOptions(Customizer<ContentTypeOptionsConfig> contentTypeOptionsCustomizer) |
| 142 | + throws Exception { |
| 143 | + contentTypeOptionsCustomizer.customize(contentTypeOptions.enable()); |
| 144 | + return HeadersConfigurer.this; |
| 145 | + } |
| 146 | + |
124 | 147 | public final class ContentTypeOptionsConfig {
|
125 | 148 | private XContentTypeOptionsHeaderWriter writer;
|
126 | 149 |
|
@@ -174,6 +197,25 @@ public XXssConfig xssProtection() {
|
174 | 197 | return xssProtection.enable();
|
175 | 198 | }
|
176 | 199 |
|
| 200 | + /** |
| 201 | + * <strong>Note this is not comprehensive XSS protection!</strong> |
| 202 | + * |
| 203 | + * <p> |
| 204 | + * Allows customizing the {@link XXssProtectionHeaderWriter} which adds the <a href= |
| 205 | + * "https://blogs.msdn.com/b/ieinternals/archive/2011/01/31/controlling-the-internet-explorer-xss-filter-with-the-x-xss-protection-http-header.aspx" |
| 206 | + * >X-XSS-Protection header</a> |
| 207 | + * </p> |
| 208 | + * |
| 209 | + * @param xssCustomizer the {@link Customizer} to provide more options for |
| 210 | + * the {@link XXssConfig} |
| 211 | + * @return the {@link HeadersConfigurer} for additional customizations |
| 212 | + * @throws Exception |
| 213 | + */ |
| 214 | + public HeadersConfigurer<H> xssProtection(Customizer<XXssConfig> xssCustomizer) throws Exception { |
| 215 | + xssCustomizer.customize(xssProtection.enable()); |
| 216 | + return HeadersConfigurer.this; |
| 217 | + } |
| 218 | + |
177 | 219 | public final class XXssConfig {
|
178 | 220 | private XXssProtectionHeaderWriter writer;
|
179 | 221 |
|
@@ -268,6 +310,26 @@ public CacheControlConfig cacheControl() {
|
268 | 310 | return cacheControl.enable();
|
269 | 311 | }
|
270 | 312 |
|
| 313 | + /** |
| 314 | + * Allows customizing the {@link CacheControlHeadersWriter}. Specifically it adds the |
| 315 | + * following headers: |
| 316 | + * <ul> |
| 317 | + * <li>Cache-Control: no-cache, no-store, max-age=0, must-revalidate</li> |
| 318 | + * <li>Pragma: no-cache</li> |
| 319 | + * <li>Expires: 0</li> |
| 320 | + * </ul> |
| 321 | + * |
| 322 | + * @param cacheControlCustomizer the {@link Customizer} to provide more options for |
| 323 | + * the {@link CacheControlConfig} |
| 324 | + * @return the {@link HeadersConfigurer} for additional customizations |
| 325 | + * @throws Exception |
| 326 | + */ |
| 327 | + public HeadersConfigurer<H> cacheControl(Customizer<CacheControlConfig> cacheControlCustomizer) throws Exception { |
| 328 | + cacheControlCustomizer.customize(cacheControl.enable()); |
| 329 | + return HeadersConfigurer.this; |
| 330 | + } |
| 331 | + |
| 332 | + |
271 | 333 | public final class CacheControlConfig {
|
272 | 334 | private CacheControlHeadersWriter writer;
|
273 | 335 |
|
@@ -319,6 +381,21 @@ public HstsConfig httpStrictTransportSecurity() {
|
319 | 381 | return hsts.enable();
|
320 | 382 | }
|
321 | 383 |
|
| 384 | + /** |
| 385 | + * Allows customizing the {@link HstsHeaderWriter} which provides support for <a |
| 386 | + * href="https://tools.ietf.org/html/rfc6797">HTTP Strict Transport Security |
| 387 | + * (HSTS)</a>. |
| 388 | + * |
| 389 | + * @param hstsCustomizer the {@link Customizer} to provide more options for |
| 390 | + * the {@link HstsConfig} |
| 391 | + * @return the {@link HeadersConfigurer} for additional customizations |
| 392 | + * @throws Exception |
| 393 | + */ |
| 394 | + public HeadersConfigurer<H> httpStrictTransportSecurity(Customizer<HstsConfig> hstsCustomizer) throws Exception { |
| 395 | + hstsCustomizer.customize(hsts.enable()); |
| 396 | + return HeadersConfigurer.this; |
| 397 | + } |
| 398 | + |
322 | 399 | public final class HstsConfig {
|
323 | 400 | private HstsHeaderWriter writer;
|
324 | 401 |
|
@@ -440,6 +517,19 @@ public FrameOptionsConfig frameOptions() {
|
440 | 517 | return frameOptions.enable();
|
441 | 518 | }
|
442 | 519 |
|
| 520 | + /** |
| 521 | + * Allows customizing the {@link XFrameOptionsHeaderWriter}. |
| 522 | + * |
| 523 | + * @param frameOptionsCustomizer the {@link Customizer} to provide more options for |
| 524 | + * the {@link FrameOptionsConfig} |
| 525 | + * @return the {@link HeadersConfigurer} for additional customizations |
| 526 | + * @throws Exception |
| 527 | + */ |
| 528 | + public HeadersConfigurer<H> frameOptions(Customizer<FrameOptionsConfig> frameOptionsCustomizer) throws Exception { |
| 529 | + frameOptionsCustomizer.customize(frameOptions.enable()); |
| 530 | + return HeadersConfigurer.this; |
| 531 | + } |
| 532 | + |
443 | 533 | public final class FrameOptionsConfig {
|
444 | 534 | private XFrameOptionsHeaderWriter writer;
|
445 | 535 |
|
@@ -516,6 +606,20 @@ public HpkpConfig httpPublicKeyPinning() {
|
516 | 606 | return hpkp.enable();
|
517 | 607 | }
|
518 | 608 |
|
| 609 | + /** |
| 610 | + * Allows customizing the {@link HpkpHeaderWriter} which provides support for <a |
| 611 | + * href="https://tools.ietf.org/html/rfc7469">HTTP Public Key Pinning (HPKP)</a>. |
| 612 | + * |
| 613 | + * @param hpkpCustomizer the {@link Customizer} to provide more options for |
| 614 | + * the {@link HpkpConfig} |
| 615 | + * @return the {@link HeadersConfigurer} for additional customizations |
| 616 | + * @throws Exception |
| 617 | + */ |
| 618 | + public HeadersConfigurer<H> httpPublicKeyPinning(Customizer<HpkpConfig> hpkpCustomizer) throws Exception { |
| 619 | + hpkpCustomizer.customize(hpkp.enable()); |
| 620 | + return HeadersConfigurer.this; |
| 621 | + } |
| 622 | + |
519 | 623 | public final class HpkpConfig {
|
520 | 624 | private HpkpHeaderWriter writer;
|
521 | 625 |
|
@@ -713,12 +817,57 @@ public ContentSecurityPolicyConfig contentSecurityPolicy(String policyDirectives
|
713 | 817 | return contentSecurityPolicy;
|
714 | 818 | }
|
715 | 819 |
|
| 820 | + /** |
| 821 | + * <p> |
| 822 | + * Allows configuration for <a href="https://www.w3.org/TR/CSP2/">Content Security Policy (CSP) Level 2</a>. |
| 823 | + * </p> |
| 824 | + * |
| 825 | + * <p> |
| 826 | + * Calling this method automatically enables (includes) the Content-Security-Policy header in the response |
| 827 | + * using the supplied security policy directive(s). |
| 828 | + * </p> |
| 829 | + * |
| 830 | + * <p> |
| 831 | + * Configuration is provided to the {@link ContentSecurityPolicyHeaderWriter} which supports the writing |
| 832 | + * of the two headers as detailed in the W3C Candidate Recommendation: |
| 833 | + * </p> |
| 834 | + * <ul> |
| 835 | + * <li>Content-Security-Policy</li> |
| 836 | + * <li>Content-Security-Policy-Report-Only</li> |
| 837 | + * </ul> |
| 838 | + * |
| 839 | + * @see ContentSecurityPolicyHeaderWriter |
| 840 | + * @param contentSecurityCustomizer the {@link Customizer} to provide more options for |
| 841 | + * the {@link ContentSecurityPolicyConfig} |
| 842 | + * @return the {@link HeadersConfigurer} for additional customizations |
| 843 | + * @throws Exception |
| 844 | + */ |
| 845 | + public HeadersConfigurer<H> contentSecurityPolicy(Customizer<ContentSecurityPolicyConfig> contentSecurityCustomizer) |
| 846 | + throws Exception { |
| 847 | + this.contentSecurityPolicy.writer = new ContentSecurityPolicyHeaderWriter(); |
| 848 | + contentSecurityCustomizer.customize(this.contentSecurityPolicy); |
| 849 | + |
| 850 | + return HeadersConfigurer.this; |
| 851 | + } |
| 852 | + |
716 | 853 | public final class ContentSecurityPolicyConfig {
|
717 | 854 | private ContentSecurityPolicyHeaderWriter writer;
|
718 | 855 |
|
719 | 856 | private ContentSecurityPolicyConfig() {
|
720 | 857 | }
|
721 | 858 |
|
| 859 | + /** |
| 860 | + * Sets the security policy directive(s) to be used in the response header. |
| 861 | + * |
| 862 | + * @param policyDirectives the security policy directive(s) |
| 863 | + * @return the {@link ContentSecurityPolicyConfig} for additional configuration |
| 864 | + * @throws IllegalArgumentException if policyDirectives is null or empty |
| 865 | + */ |
| 866 | + public ContentSecurityPolicyConfig policyDirectives(String policyDirectives) { |
| 867 | + this.writer.setPolicyDirectives(policyDirectives); |
| 868 | + return this; |
| 869 | + } |
| 870 | + |
722 | 871 | /**
|
723 | 872 | * Enables (includes) the Content-Security-Policy-Report-Only header in the response.
|
724 | 873 | *
|
@@ -860,13 +1009,50 @@ public ReferrerPolicyConfig referrerPolicy(ReferrerPolicy policy) {
|
860 | 1009 | return this.referrerPolicy;
|
861 | 1010 | }
|
862 | 1011 |
|
| 1012 | + /** |
| 1013 | + * <p> |
| 1014 | + * Allows configuration for <a href="https://www.w3.org/TR/referrer-policy/">Referrer Policy</a>. |
| 1015 | + * </p> |
| 1016 | + * |
| 1017 | + * <p> |
| 1018 | + * Configuration is provided to the {@link ReferrerPolicyHeaderWriter} which support the writing |
| 1019 | + * of the header as detailed in the W3C Technical Report: |
| 1020 | + * </p> |
| 1021 | + * <ul> |
| 1022 | + * <li>Referrer-Policy</li> |
| 1023 | + * </ul> |
| 1024 | + * |
| 1025 | + * @see ReferrerPolicyHeaderWriter |
| 1026 | + * @param referrerPolicyCustomizer the {@link Customizer} to provide more options for |
| 1027 | + * the {@link ReferrerPolicyConfig} |
| 1028 | + * @return the {@link HeadersConfigurer} for additional customizations |
| 1029 | + * @throws Exception |
| 1030 | + */ |
| 1031 | + public HeadersConfigurer<H> referrerPolicy(Customizer<ReferrerPolicyConfig> referrerPolicyCustomizer) throws Exception { |
| 1032 | + this.referrerPolicy.writer = new ReferrerPolicyHeaderWriter(); |
| 1033 | + referrerPolicyCustomizer.customize(this.referrerPolicy); |
| 1034 | + return HeadersConfigurer.this; |
| 1035 | + } |
| 1036 | + |
863 | 1037 | public final class ReferrerPolicyConfig {
|
864 | 1038 |
|
865 | 1039 | private ReferrerPolicyHeaderWriter writer;
|
866 | 1040 |
|
867 | 1041 | private ReferrerPolicyConfig() {
|
868 | 1042 | }
|
869 | 1043 |
|
| 1044 | + /** |
| 1045 | + * Sets the policy to be used in the response header. |
| 1046 | + * |
| 1047 | + * @param policy a referrer policy |
| 1048 | + * @return the {@link ReferrerPolicyConfig} for additional configuration |
| 1049 | + * @throws IllegalArgumentException if policy is null |
| 1050 | + */ |
| 1051 | + public ReferrerPolicyConfig policy(ReferrerPolicy policy) { |
| 1052 | + this.writer.setPolicy(policy); |
| 1053 | + return this; |
| 1054 | + } |
| 1055 | + |
870 | 1056 | public HeadersConfigurer<H> and() {
|
871 | 1057 | return HeadersConfigurer.this;
|
872 | 1058 | }
|
|
0 commit comments