配置安全标头与 HTTPS
通过 HTTP 安全标头、HSTS 和强制 HTTPS 加固生产环境中的 Spring 应用,抵御常见的传输层和浏览器攻击
配置安全标头与 HTTPS 是 CoddyKit 上的免费 Spring Security 6 & JWT Authentication 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Spring Security 6 & JWT Authentication 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Spring Security 6 & JWT Authentication 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
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.
常见问题解答
「配置安全标头与 HTTPS」课时是免费的吗?
是的 — 「配置安全标头与 HTTPS」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Spring Security 6 & JWT Authentication 课程的其余内容,请升级到 CoddyKit PRO。 Spring Security 6 & JWT Authentication 课程共包含 4 节课。
「配置安全标头与 HTTPS」这节课中我会学到什么?
通过 HTTP 安全标头、HSTS 和强制 HTTPS 加固生产环境中的 Spring 应用,抵御常见的传输层和浏览器攻击 你通过在浏览器中直接运行的动手代码来练习 Spring Security 6 & JWT Authentication,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Spring Security 6 & JWT Authentication 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Spring Security 6 & JWT Authentication 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「配置安全标头与 HTTPS」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Spring Security 6 & JWT Authentication 课中编写并运行代码吗?
能。每节 Spring Security 6 & JWT Authentication 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 生产环境安全加固
- 安全事件日志记录与监控
- 常见安全漏洞与修复方案
- 配置安全标头与 HTTPS