Spring Security 6 & JWT Authentication · Lezione

Configurare gli header di sicurezza e HTTPS

Rafforzi la sua applicazione Spring in produzione con gli header di sicurezza HTTP, HSTS e HTTPS obbligatorio, per difendersi dai comuni attacchi al trasporto e al browser.

Lezione 4 di 413 passaggi

Configurare gli header di sicurezza e HTTPS è una lezione Spring Security 6 & JWT Authentication gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Spring Security 6 & JWT Authentication, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Spring Security 6 & JWT Authentication include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

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.com

Quick Check

Test your understanding of security headers.

Recap

You learned to harden the transport and browser layer:

  • Force HTTPS with requiresChannel and 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.

Gratis per iniziare

Impara Java con un tutor IA — gratis

Scrivi ed esegui vero codice nel tuo browser, ricevi aiuto istantaneo da un tutor IA disponibile 24/7, e riprendi da dove hai lasciato sul web o nell'app.

Corsi
12
Lezioni
48

Domande Frequenti

La lezione «Configurare gli header di sicurezza e HTTPS» è gratuita?

Sì — il testo completo di «Configurare gli header di sicurezza e HTTPS» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Spring Security 6 & JWT Authentication, passa a CoddyKit PRO. Il corso Spring Security 6 & JWT Authentication include 4 lezioni in totale.

Cosa imparerò in «Configurare gli header di sicurezza e HTTPS»?

Rafforzi la sua applicazione Spring in produzione con gli header di sicurezza HTTP, HSTS e HTTPS obbligatorio, per difendersi dai comuni attacchi al trasporto e al browser. Eserciti Spring Security 6 & JWT Authentication con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare Spring Security 6 & JWT Authentication?

Non è richiesta alcuna esperienza precedente. Spring Security 6 & JWT Authentication su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.

Quanto tempo richiede la lezione «Configurare gli header di sicurezza e HTTPS»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione Spring Security 6 & JWT Authentication?

Sì. Ogni lezione Spring Security 6 & JWT Authentication include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Rafforzamento della sicurezza in produzione
  2. Logging e monitoraggio degli eventi di sicurezza
  3. Vulnerabilità comuni e relative correzioni
  4. Configurare gli header di sicurezza e HTTPS
← Torna a Spring Security 6 & JWT Authentication