一、 Cookie和Session的發展史
一開始:只有一個頁面,沒有登錄功能,大家看到東西都一樣
新聞
時代發展,出現了需要登錄注冊的網站,要有一門技術存儲我們的登錄信息
京東、天貓
cookie
存儲形式:k:v鍵值對
存儲位置:客戶端
不安全,信息可能會泄露
時代發展,需要有一門新的安全的技術
session
標識符,來表示我是當前用戶加密出來的數據
對敏感信息進行加密處理
存儲服務端:
標識符配合上你的加密串
把我的標識符+ 字符串全給客戶端
客戶端存儲格式
session_id:返回回來的表示符+加密串
token
三段式加密
?二、Cookie
2.1?設置cookie
HttpResponse
render
redriectobj = HttpResponse("ok")
obj.set_cookie('k','v')
def login(request, *args, **kwargs):if request.method == 'POST':username = request.POST.get("username")password = request.POST.get("password")if username == "dream" and password == "521":obj = HttpResponse("ok")obj.set_cookie('sign', 'user')return objelse:return redirect('/login/')return render(request, 'login.html')
2.2 取值
cookie取值驗證:
def home(request, *args, **kwargs):sign = request.COOKIES.get('sign')if sign and sign == 'user':return HttpResponse("這是home頁面")else:return redirect('/login/')
?完整版 cookie登錄注冊:
def login(request, *args, **kwargs):# next_url = request.get_full_path()# print(next_url) # /login/?next_url=/home/if request.method == 'POST':username = request.POST.get("username")password = request.POST.get("password")if username == "dream" and password == "521":next_url = request.GET.get('next_url')# print(next_url) # /home/obj = redirect(next_url)obj.set_cookie('sign', 'user')return objelse:return redirect('/login/')return render(request, 'login.html')def login_auth(func):def inner(request, *args, **kwargs):# print(request.path_info) # /home/# print(request.get_full_path()) # /home/?username=111next_url = request.get_full_path() # /home/# print(next_url)# /home/sign = request.COOKIES.get('sign')if sign and sign == 'user':res = func(request, *args, **kwargs)return reselse:return redirect(f'/login/?next_url={next_url}')return inner@login_auth def home(request, *args, **kwargs):return HttpResponse("這是home頁面")# def home(request, *args, **kwargs): # sign = request.COOKIES.get('sign') # if sign and sign == 'user': # return HttpResponse("這是home頁面") # else: # return redirect('/login/')@login_auth def index(request, *args, **kwargs):return HttpResponse("這是index頁面")
2.3??設置過期時間
obj.set_cookie('sign', 'user', expires=3) obj.set_cookie('sign', 'user', max_age=3)
2.4??刪除cookie
def logout(request, *args, **kwargs):obj = redirect('/home/')# 設置超時時間 5s 到期obj.delete_cookie('sign')return obj
三、??Session
3.1?設置session
request.session['sign'] = 'user'
3.2??取值session
sign = request.session.get('sign')
def login(request, *args, **kwargs):# next_url = request.get_full_path()# print(next_url) # /login/?next_url=/home/if request.method == 'POST':username = request.POST.get("username")password = request.POST.get("password")if username == "dream" and password == "521":# next_url = request.GET.get('next_url')# print(next_url) # /home/request.session['sign'] = 'user'obj = redirect('/home/')# 設置過期時間# obj.set_cookie('sign', 'user', expires=3)# obj.set_cookie('sign', 'user', max_age=3)return objelse:return redirect('/login/')return render(request, 'login.html')def login_auth(func):def inner(request, *args, **kwargs):# print(request.path_info) # /home/# print(request.get_full_path()) # /home/?username=111next_url = request.get_full_path() # /home/# print(next_url)# /home/sign = request.session.get('sign')# print(sign) # userif sign and sign == 'user':res = func(request, *args, **kwargs)return reselse:return redirect(f'/login/?next_url={next_url}')return inner@login_auth def home(request, *args, **kwargs):return HttpResponse("這是home頁面")
注:?
session基于數據庫表才能使用的
必須先遷移數據庫,生成 django_session 表
session只對當次登錄有效
主動清除瀏覽器中本地存在的session
驗簽發現,沒有sessionid就會自動生成新的session
django_sessoin
表中的數據條數取決于瀏覽器同一個計算機(IP地址)上同一個瀏覽器只會有一條數據生效
同一個計算機(IP地址)上多個瀏覽器會有多個數據生效
當session過期的時候,可能會出現多條數據對應一個瀏覽器
但是這些數據不會持久化存儲,會被定時清理掉,可以手動清除也可以代碼清除
目的是為了節省服務器數據庫資源
3.3??session設置過期時間
request.session['sign'] = 'user'# 如果是數字的話就是指定 s shu# request.session.set_expiry(3)# 0 就是關閉瀏覽器后自動清除瀏覽器的sessionidrequest.session.set_expiry(0)
3.4??刪除session
# 刪除session方式一# request.session.delete()# 把瀏覽器和數據庫里面的session全部清除掉request.session.flush()
四、??CBV加裝飾器的三種方法
?4.1?方式一:加載視圖函數上面
@method_decorator(login_auth)def get(self, request, *args, **kwargs):return HttpResponse("這是home頁面")def post(self):...
4.2???方式二:放在類視圖上面 (放的裝飾器函數,name指定你的視圖函數里面的方法)
@method_decorator(login_auth, name='get') @method_decorator(login_auth, name='post') class UserView(View):
4.3???方式三 : dispactch 方法加裝飾器 : 本視圖函數內所有的視圖都需要走裝飾器
@method_decorator(login_auth)def dispatch(self, request, *args, **kwargs):# Try to dispatch to the right method; if a method doesn't exist,# defer to the error handler. Also defer to the error handler if the# request method isn't on the approved list.if request.method.lower() in self.http_method_names:handler = getattr(self, request.method.lower(), self.http_method_not_allowed)else:handler = self.http_method_not_allowedreturn handler(request, *args, **kwargs)