Skip to content

Commit 3fcc95d

Browse files
committed
Add preload support to Strict-Transport-Security
1. Preload support in Servlet Security(XML & Java) 2. Preload support in Reactive Security 3. Test for preload support in Servlet Security 4. Test for preload support in Reactive Security Fixes: gh-6312
1 parent be23ab8 commit 3fcc95d

File tree

13 files changed

+3857
-18
lines changed

13 files changed

+3857
-18
lines changed

config/src/main/java/org/springframework/security/config/annotation/web/configurers/HeadersConfigurer.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,25 @@ public HstsConfig includeSubDomains(boolean includeSubDomains) {
379379
return this;
380380
}
381381

382+
/**
383+
* <p>
384+
* If true, preload will be included in HSTS Header. The default is false.
385+
* </p>
386+
*
387+
* <p>
388+
* See <a href="https://hstspreload.org/">Website hstspreload.org</a>
389+
* for additional details.
390+
* </p>
391+
*
392+
* @param preload true to include preload, else false
393+
* @since 5.2.0
394+
* @author Ankur Pathak
395+
*/
396+
public HstsConfig preload(boolean preload) {
397+
writer.setPreload(preload);
398+
return this;
399+
}
400+
382401
/**
383402
* Disables Strict Transport Security
384403
*

config/src/main/java/org/springframework/security/config/http/HeadersBeanDefinitionParser.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public class HeadersBeanDefinitionParser implements BeanDefinitionParser {
6868
private static final String ATT_INCLUDE_SUBDOMAINS = "include-subdomains";
6969
private static final String ATT_MAX_AGE_SECONDS = "max-age-seconds";
7070
private static final String ATT_REQUEST_MATCHER_REF = "request-matcher-ref";
71+
private static final String ATT_PRELOAD = "preload";
7172
private static final String ATT_REPORT_ONLY = "report-only";
7273
private static final String ATT_REPORT_URI = "report-uri";
7374
private static final String ATT_ALGORITHM = "algorithm";
@@ -194,6 +195,14 @@ private void addHsts(boolean addIfNotPresent, Element hstsElement,
194195
}
195196
headersWriter.addPropertyReference("requestMatcher", requestMatcherRef);
196197
}
198+
String preload = hstsElement.getAttribute(ATT_PRELOAD);
199+
if (StringUtils.hasText(preload)) {
200+
if (disabled) {
201+
attrNotAllowed(context, ATT_PRELOAD, ATT_DISABLED,
202+
hstsElement);
203+
}
204+
headersWriter.addPropertyValue("preload", preload);
205+
}
197206

198207
if (disabled == true) {
199208
return;

config/src/main/java/org/springframework/security/config/web/server/ServerHttpSecurity.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2367,6 +2367,26 @@ public HstsSpec includeSubdomains(boolean includeSubDomains) {
23672367
return this;
23682368
}
23692369

2370+
/**
2371+
* <p>
2372+
* Configures if preload should be included. Default is false
2373+
* </p>
2374+
*
2375+
* <p>
2376+
* See <a href="https://hstspreload.org/">Website hstspreload.org</a>
2377+
* for additional details.
2378+
* </p>
2379+
*
2380+
* @param preload if subdomains should be included
2381+
* @return the {@link HstsSpec} to continue configuring
2382+
* @since 5.2.0
2383+
* @author Ankur Pathak
2384+
*/
2385+
public HstsSpec preload(boolean preload) {
2386+
HeaderSpec.this.hsts.setPreload(preload);
2387+
return this;
2388+
}
2389+
23702390
/**
23712391
* Allows method chaining to continue configuring the {@link ServerHttpSecurity}
23722392
* @return the {@link HeaderSpec} to continue configuring

config/src/main/resources/org/springframework/security/config/spring-security-5.2.rnc

Lines changed: 919 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)