Skip to content

Commit b7ed919

Browse files
ankurpathakrwinch
authored andcommitted
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 739594d commit b7ed919

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

+19
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

+9
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

+20
Original file line numberDiff line numberDiff line change
@@ -2375,6 +2375,26 @@ public HstsSpec includeSubdomains(boolean includeSubDomains) {
23752375
return this;
23762376
}
23772377

2378+
/**
2379+
* <p>
2380+
* Configures if preload should be included. Default is false
2381+
* </p>
2382+
*
2383+
* <p>
2384+
* See <a href="https://hstspreload.org/">Website hstspreload.org</a>
2385+
* for additional details.
2386+
* </p>
2387+
*
2388+
* @param preload if subdomains should be included
2389+
* @return the {@link HstsSpec} to continue configuring
2390+
* @since 5.2.0
2391+
* @author Ankur Pathak
2392+
*/
2393+
public HstsSpec preload(boolean preload) {
2394+
HeaderSpec.this.hsts.setPreload(preload);
2395+
return this;
2396+
}
2397+
23782398
/**
23792399
* Allows method chaining to continue configuring the {@link ServerHttpSecurity}
23802400
* @return the {@link HeaderSpec} to continue configuring

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

+919
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)