Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

Which HTTP security headers does Spring Security add by default, and which important header must you configure yourself?

By default Spring Security adds Cache-Control, Pragma and Expires headers that stop caching of responses, X-Content-Type-Options: nosniff, X-Frame-Options: DENY, Strict-Transport-Security on HTTPS requests only, and, since Spring Security 6, X-XSS-Protection: 0, which disables the old browser filter. The important header it does not add is Content-Security-Policy, because a useful policy depends on the application; we configure it ourselves with headers(h -> h.contentSecurityPolicy(...)).

Why this question is asked

Security headers are cheap protection: a few lines in every response that tell the browser to refuse behaviour attackers rely on. Spring Security turns most of them on without asking, which is good, but it also means many developers never learn what they are or notice the one that is missing. Interviewers ask this to check whether we know what our framework does for us and where its defaults stop. In this answer, we will look at a default response, go through each header and the attack it addresses, explain why X-XSS-Protection is now set to 0, and then add the Content-Security-Policy that we must configure ourselves.

1What a Default Response Looks Like

With Spring Security on the classpath and no header configuration, an authenticated page served over HTTPS comes back with these headers.

Java
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
X-Content-Type-Options: nosniff
Strict-Transport-Security: max-age=31536000 ; includeSubDomains
X-Frame-Options: DENY
X-XSS-Protection: 0
// no Content-Security-Policy, no Referrer-Policy

These come from the HeaderWriterFilter, which Spring Security includes by default. Each header is a separate writer and can be configured or turned off through http.headers(...). Exact values can differ slightly between versions, so check the reference for yours.

2Cache Headers: Not Storing Private Pages

Cache-Control, Pragma and Expires together tell browsers and proxies not to store the response. The reason is shared and leftover copies. Without them, a bank statement page might be kept in the browser cache and shown again to the next person on a shared computer who presses Back, or kept by a proxy. Pragma and Expires are older forms for HTTP/1.0 clients; Cache-Control is the modern one.

Spring Security only writes these when the application has not set its own cache headers, so a controller that serves a public image with a long max-age keeps its caching. For static resources, Spring Boot's resource handling sets its own cache rules.

3Nosniff and Frame Protection

X-Content-Type-Options: nosniff stops the browser from guessing a response's type from its content, called MIME sniffing. Without it, a file uploaded as text/plain but containing HTML or script could be interpreted as HTML or JavaScript, turning an upload feature into a way to run script on our origin. With nosniff, the browser trusts the declared Content-Type and refuses to run a script or stylesheet whose type does not match.

X-Frame-Options: DENY stops other sites from showing our pages inside a frame. That blocks clickjacking, where an attacker loads our page invisibly in a frame over a harmless-looking button, so the user's click actually lands on, say, our Delete account button. If our own application needs to frame its pages, SAMEORIGIN allows that while still blocking other sites. The modern equivalent is the CSP directive frame-ancestors, which is more flexible.

4HSTS on HTTPS Requests Only

Strict-Transport-Security (HSTS) tells the browser to use HTTPS for our domain for the given time, here one year, even if the user types http:// or follows an old link. That closes the window in which an attacker on the network could intercept the first plain HTTP request and redirect the user to a fake site.

Spring Security writes it only when the request itself arrived over HTTPS. The reason is the specification: browsers ignore HSTS on plain HTTP, and sending it there would be meaningless. A common surprise follows from this. Behind a load balancer that terminates TLS, the application sees plain HTTP and omits the header, unless it trusts the X-Forwarded-Proto header, for example with server.forward-headers-strategy=native or framework in Spring Boot.

5Why X-XSS-Protection Is Set to 0

Older browsers had a built-in XSS filter that tried to detect reflected scripts and block or remove them, and X-XSS-Protection: 1; mode=block switched it on. It turned out that the filter itself could be abused: attackers could trigger it on purpose to remove legitimate scripts or leak information across sites. Browsers have since removed it, and current guidance is to disable it.

So Spring Security 6 sends X-XSS-Protection: 0, which turns the filter off in the old browsers that still have it. This is not a weakening: real XSS protection comes from output encoding and a Content-Security-Policy, not from that filter.

6The Header We Must Configure: Content-Security-Policy

Content-Security-Policy (CSP) tells the browser which sources of script, style, images, frames and connections the page may use. A strict policy means that even if an attacker injects a <script> tag, the browser refuses to run it because it does not come from an allowed source or carry the right nonce. It is one of the strongest defences against XSS impact.

Spring Security cannot add a CSP by default because any useful policy depends on the application: which scripts it loads, which CDNs, which analytics, whether it uses inline scripts. A generic policy would either break most applications or allow so much that it protected nothing. So we write it ourselves.

Java
http.headers(h -> h
.contentSecurityPolicy(csp -> csp.policyDirectives(
"default-src 'self'; script-src 'self'; object-src 'none'; " +
"frame-ancestors 'none'; base-uri 'self'; form-action 'self'"))
.referrerPolicy(r -> r.policy(ReferrerPolicyHeaderWriter.ReferrerPolicy.STRICT_ORIGIN_WHEN_CROSS_ORIGIN)));
// response now also has:
// Content-Security-Policy: default-src 'self'; script-src 'self'; object-src 'none'; ...
// Referrer-Policy: strict-origin-when-cross-origin
Default headers and what they stop
HeaderDefault valueStops
Cache-Control / Pragma / Expiresno-store and friendsPrivate pages kept in caches
X-Content-Type-OptionsnosniffUploaded files run as script or HTML
X-Frame-OptionsDENYClickjacking through frames
Strict-Transport-Security1 year, HTTPS onlyDowngrade to plain HTTP
X-XSS-Protection0Abuse of the old browser filter
Content-Security-PolicyNot setMust be configured: XSS impact

For an existing application, the policy is usually introduced in report-only mode first, with .reportOnly(), so violations are reported without breaking pages. Referrer-Policy is the other header worth adding explicitly, so full URLs with ids or tokens are not sent to other sites in the Referer header.

7Follow-up questions

Why is my HSTS header missing in production?

Most likely TLS ends at the load balancer, so the application sees HTTP and skips the header. Configure Spring Boot to trust forwarded headers from the proxy, or set HSTS at the load balancer.

Can I turn off one default header without losing the others?

Yes. Each writer can be configured separately, for example headers(h -> h.frameOptions(f -> f.sameOrigin())) to allow same-origin framing. Avoid headers(h -> h.disable()), which removes all of them.

Should I still send X-Frame-Options if I use CSP frame-ancestors?

Sending both is common. Modern browsers prefer frame-ancestors, and X-Frame-Options covers older ones. Keep them consistent so they do not contradict each other.

8Conclusion

In this answer, we went through Spring Security's default headers: cache headers that keep private pages out of caches, nosniff against MIME sniffing, X-Frame-Options: DENY against clickjacking, HSTS on HTTPS requests only, and X-XSS-Protection: 0 because the old browser filter caused more harm than good. Content-Security-Policy is not added by default because it depends on the application, so we configure it with headers(h -> h.contentSecurityPolicy(...)), ideally starting in report-only mode, and add a Referrer-Policy.

What to say in the interviewAbout 80 seconds aloud

Open with

By default Spring Security's header filter adds several headers. Cache-Control with no-store and must-revalidate, plus Pragma no-cache and Expires zero, so authenticated pages are not kept in the browser cache or proxies; it skips these if the application already set its own cache headers.

Then, if they want more

X-Content-Type-Options nosniff, so the browser trusts the declared content type and an uploaded file cannot be run as script. X-Frame-Options DENY, which blocks clickjacking by not letting other sites frame our pages. Strict-Transport-Security for a year, but only on requests that came over HTTPS, which is why it often goes missing behind a load balancer unless forwarded headers are trusted.

And since Spring Security 6, X-XSS-Protection set to zero, because the old browser XSS filter could itself be abused and browsers have dropped it. The important one it does not add is Content-Security-Policy. It cannot, because a useful policy depends on which scripts, styles and third parties the application uses.

So I configure it with http.headers and contentSecurityPolicy, for example default-src self, script-src self, object-src none and frame-ancestors none, and for an existing application I would start in report-only mode to find violations first. I would also add a Referrer-Policy such as strict-origin-when-cross-origin, which Spring Security can write but does not add by default.

About this answer

Last updated
12 November 2026
Versions it names
Spring Security 6
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
OWASP Top Ten
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Which HTTP security headers does Spring Security add by default, and which important...": https://scrivio-interview-library.netlify.app/spring-boot/web-security-threats/5bb14b3d8d/