HSTS – HTTP Strict Transport Security for Java APIs
Fast recall
Quick revision
Core answer
Strict-Transport-Security response header that tells supporting browsers to use HTTPS for a host for a stated max-age. After the browser receives the policy over a valid HTTPS connection, it upgrades later HTTP navigations and refuses click-through on certificate errors. It does not protect the first visit unless the site is preloaded, and most non-browser API clients do not use browser HSTS state. includeSubDomains and preload should be enabled only after every affected host is ready for permanent HTTPS.Interview-ready guide
Interview answer
HSTS is a browser policy sent in the Strict-Transport-Security response header. After a browser receives it over a valid HTTPS connection, it remembers the host for the stated max-age. Later HTTP navigations are upgraded locally to HTTPS, and the browser will not offer a click-through when the certificate is invalid.
That helps stop SSL stripping after the browser has learned the policy. A normal HSTS header does not protect a first visit that starts over HTTP. Browser preload can cover that gap, but preload and includeSubDomains should be used only after every affected host supports HTTPS because removal is slow and a missed subdomain can become unreachable.
HSTS is mainly a browser behavior. A mobile client, command-line tool, or backend HTTP library may ignore it, so API integrations must be configured with an HTTPS base URL and proper certificate validation. The server should still redirect or close plaintext HTTP according to its deployment plan.
HSTS – HTTP Strict Transport Security for Java APIs
Direct Answer
HSTS is the Strict-Transport-Security response header that tells supporting browsers to use HTTPS for a host for a stated max-age. After the browser receives the policy over a valid HTTPS connection, it upgrades later HTTP navigations and refuses click-through on certificate errors. It does not protect the first visit unless the site is preloaded, and most non-browser API clients do not use browser HSTS state. includeSubDomains and preload should be enabled only after every affected host is ready for permanent HTTPS.
Code Example
@Bean
SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.headers(headers -> headers
.httpStrictTransportSecurity(hsts -> hsts
.includeSubDomains(true)
.preload(true)
.maxAgeInSeconds(31536000))); // 1 year
return http.build();
}
// Response header sent over HTTPS:
// Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
Spring Security adds HSTS by default on HTTPS responses; this customizes the directives. Browsers ignore HSTS sent over plain HTTP.
Common Mistakes & Pitfalls
- Confusing HSTS with the certificate: HSTS is a policy header; the certificate is the identity proof. Different things.
- Enabling
preloadtoo early: getting onto the preload list is hard to undo — only do it when HTTPS works everywhere. - Huge
max-agebefore you're ready: if HTTPS breaks, users are locked out for that whole duration. - Sending HSTS over HTTP: browsers ignore it on plaintext; it only takes effect over HTTPS.
- Forgetting subdomains: without
includeSubDomains, anhttp://api.request can still be stripped.
Check your understanding
Answer these without looking back at the article.
- What is an SSL-stripping attack and how does HSTS prevent it?
- Why can enabling HSTS preload be risky if your HTTPS isn't solid?
- What does includeSubDomains add to the policy?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.