Configuring Security Headers and HTTPS
Harden your production Spring app with HTTP security headers, HSTS, and enforced HTTPS to defend against common transport and browser-based attacks.
Configuring Security Headers and HTTPS is a free Spring Security 6 & JWT Authentication lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Spring Security 6 & JWT Authentication learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Defense at the Transport Layer
Even a well-secured backend is exposed if traffic travels unencrypted or the browser mishandles your responses. Security headers and HTTPS close these gaps at the transport and browser layer.
Why HTTPS Is Non-Negotiable
Over plain HTTP, tokens and credentials can be read or modified by anyone on the network. HTTPS encrypts traffic and verifies the server identity, and is mandatory wherever JWTs travel.
Forcing HTTPS in Spring
Use requiresChannel to redirect any HTTP request to HTTPS automatically.
http.requiresChannel(c -> c.anyRequest().requiresSecure());HSTS
HTTP Strict Transport Security tells browsers to only ever use HTTPS for your domain, preventing downgrade attacks. Spring enables it by default for secure requests.
http.headers(h -> h
.httpStrictTransportSecurity(hsts -> hsts
.maxAgeInSeconds(31536000)
.includeSubDomains(true)));Content Security Policy
A Content-Security-Policy header limits which sources of scripts and styles the browser will load, a strong defense against cross-site scripting (XSS).
http.headers(h -> h
.contentSecurityPolicy(c -> c
.policyDirectives("default-src 'self'")));Clickjacking Protection
The X-Frame-Options header stops your pages from being embedded in iframes on other sites, blocking clickjacking. Spring sets DENY by default.
http.headers(h -> h
.frameOptions(f -> f.deny()));Preventing MIME Sniffing
The X-Content-Type-Options: nosniff header stops browsers from guessing content types, which can turn an uploaded file into executable script. It is on by default in Spring Security.
Referrer Policy
The Referrer-Policy header controls how much URL information leaks to other sites when users follow links, protecting tokens or ids that might sit in URLs.
http.headers(h -> h
.referrerPolicy(r -> r.policy(
ReferrerPolicy.SAME_ORIGIN)));Disabling the Cache for Sensitive Pages
Spring adds cache-control headers to keep authenticated responses out of browser and proxy caches, so a logged-out user on a shared machine cannot hit Back to see private data.
Cookies for Tokens
If you store tokens in cookies, mark them HttpOnly (JS cannot read), Secure (HTTPS only), and SameSite to mitigate XSS and CSRF.
Cookie c = new Cookie('token', value);
c.setHttpOnly(true);
c.setSecure(true);Verifying Your Headers
After deploying, scan your site with tools like securityheaders.com or curl to confirm each header is present and correctly valued. Trust nothing until you have checked the live response.
curl -I https://yourapp.example.comQuick Check
Test your understanding of security headers.
Recap
You learned to harden the transport and browser layer:
- Force HTTPS with
requiresChanneland enable HSTS - Use CSP, X-Frame-Options, and nosniff to block XSS and clickjacking
- Set HttpOnly, Secure, SameSite on token cookies
- Verify headers on the live deployment
These headers add cheap, high-value protection in production.
Frequently asked questions
Is the “Configuring Security Headers and HTTPS” lesson free?
Yes — the full text of “Configuring Security Headers and HTTPS” is free to read here on the web, and the Spring Security 6 & JWT Authentication course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Spring Security 6 & JWT Authentication course, upgrade to CoddyKit PRO.
What will I learn in “Configuring Security Headers and HTTPS”?
Harden your production Spring app with HTTP security headers, HSTS, and enforced HTTPS to defend against common transport and browser-based attacks. You practise Spring Security 6 & JWT Authentication with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Spring Security 6 & JWT Authentication?
No prior experience is required. Spring Security 6 & JWT Authentication on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Configuring Security Headers and HTTPS” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Spring Security 6 & JWT Authentication lesson?
Yes. Every Spring Security 6 & JWT Authentication lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Production Security Hardening
- Logging and Monitoring Security Events
- Common Security Vulnerabilities & Fixes
- Configuring Security Headers and HTTPS