การกำหนดส่วนหัวความปลอดภัยและ HTTPS
เสริมความแข็งแกร่งให้แอป Spring ในสภาพแวดล้อมใช้งานจริงด้วยส่วนหัวความปลอดภัยของ HTTP, HSTS และการบังคับใช้ HTTPS เพื่อป้องกันการโจมตีทั่วไปผ่านการรับส่งข้อมูลและเบราว์เซอร์
การกำหนดส่วนหัวความปลอดภัยและ HTTPS เป็นบทเรียน Spring Security 6 & JWT Authentication ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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.
เรียนรู้ Java ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 12
- บทเรียน
- 48
คำถามที่พบบ่อย
บทเรียน “การกำหนดส่วนหัวความปลอดภัยและ HTTPS” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การกำหนดส่วนหัวความปลอดภัยและ HTTPS” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Spring Security 6 & JWT Authentication ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Spring Security 6 & JWT Authentication มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การกำหนดส่วนหัวความปลอดภัยและ HTTPS”
เสริมความแข็งแกร่งให้แอป Spring ในสภาพแวดล้อมใช้งานจริงด้วยส่วนหัวความปลอดภัยของ HTTP, HSTS และการบังคับใช้ HTTPS เพื่อป้องกันการโจมตีทั่วไปผ่านการรับส่งข้อมูลและเบราว์เซอร์ คุณปฏิบัติ Spring Security 6 & JWT Authentication ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Spring Security 6 & JWT Authentication หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Spring Security 6 & JWT Authentication บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การกำหนดส่วนหัวความปลอดภัยและ HTTPS” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Spring Security 6 & JWT Authentication นี้ได้ไหม
ได้ บทเรียน Spring Security 6 & JWT Authentication ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การเสริมความปลอดภัยสำหรับระบบจริง
- การบันทึกล็อกและเฝ้าติดตามเหตุการณ์ด้านความปลอดภัย
- ช่องโหว่ด้านความปลอดภัยที่พบบ่อยและวิธีแก้ไข
- การกำหนดส่วนหัวความปลอดภัยและ HTTPS