SECRET_KEY e cookie firmati
Scopra come Flask firma in modo sicuro i dati della sessione.
SECRET_KEY e cookie firmati è una lezione Flask Academy gratuita su CoddyKit. Questa è la lezione 2 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 Flask Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Flask Academy include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
Where Sessions Live
By default Flask stores the whole session inside a cookie in the user's browser, not on your server. That has big implications.
The Tampering Problem
If data lives in the browser, a user could edit it. Flask stops that by signing the cookie so changes are detectable.
Meet the SECRET_KEY
Signing needs a secret. The SECRET_KEY is the private value Flask uses to stamp and verify every session cookie. 🔑
app.config['SECRET_KEY'] = 'change-me'No Key, No Sessions
Touch the session without a SECRET_KEY set and Flask raises a RuntimeError. It refuses to sign with nothing.
Signed, Not Encrypted
Important: a default session cookie is signed, not encrypted. Anyone can read it, so never store true secrets inside.
How Signing Verifies
On each request Flask recomputes the signature from the cookie and the key. A mismatch means tampering, so the session is rejected.
Make It Long and Random
A strong SECRET_KEY should be long and unpredictable. Generate one with the secrets module rather than typing a word.
import secrets
key = secrets.token_hex(32)Never Hardcode in Git
Keep the SECRET_KEY out of source control. Load it from an environment variable so each deploy has its own value.
import os
app.config['SECRET_KEY'] = os.environ['SECRET_KEY']Rotating the Key
Change the SECRET_KEY and every existing session signature stops matching. All users get logged out at once.
Same Key Everywhere
Run several app instances behind a load balancer? They must share one SECRET_KEY or each will reject the others' cookies.
Server-Side Alternative
Need bigger or hidden session data? Extensions like Flask-Session keep it on the server and store only an id in the cookie.
Quick Check
Think about what the default Flask session cookie actually protects.
Recap
You saw that SECRET_KEY signs session cookies to catch tampering. Make it long, load it from the environment, and never commit it. 🔐
Domande Frequenti
La lezione «SECRET_KEY e cookie firmati» è gratuita?
Sì — il testo completo di «SECRET_KEY e cookie firmati» è 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 Flask Academy, passa a CoddyKit PRO. Il corso Flask Academy include 4 lezioni in totale.
Cosa imparerò in «SECRET_KEY e cookie firmati»?
Scopra come Flask firma in modo sicuro i dati della sessione. Eserciti Flask Academy 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 Flask Academy?
Non è richiesta alcuna esperienza precedente. Flask Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 2 di 4.
Quanto tempo richiede la lezione «SECRET_KEY e cookie firmati»?
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 Flask Academy?
Sì. Ogni lezione Flask Academy 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
- Impostare e leggere il dizionario session
- SECRET_KEY e cookie firmati
- Impostare cookie personalizzati in una risposta
- Messaggi flash tra le richieste