一:會話技術:
1:什么是會話跟蹤技術:
首先我們應該知道,什么是會話。當瀏覽器(客戶端)第一次訪問某個網站的時候(服務器)。該瀏覽器便和服務器建立了單獨的會話。直到瀏覽器關閉或服務器斷開。但是一個網站有許多請求,如/login/,/index/等,而http協議是一種無狀態的協議,一次請求一次鏈接,並不會保存狀態信息。而我們一些信息,常常需要多個請求之間進行交互。既然請求無法保存登陸信息,那么可以使用會話跟蹤技術來保存數據信息,進而進行多個請求之間的數據互通。實現會話跟蹤技術就可以使用cookie和session來嗎實現。
二:cookie
1:什么是cookie
cookie是存在瀏覽器上的一種對象,用於保存瀏覽器的一些用戶信息,比如登陸信息,包括登陸狀態,上一次登陸時間。
優點:在用戶二次登陸的情況下,可以的快速獲取用戶登陸狀態。
缺點:因為是保存在瀏覽器端所以安全性差
保存方式:采用鍵值對的方式保存:{key:value,key:value}
2:django的cookie實現
1:設置cookie
def log_in(request): if request.method=='POST': user = request.POST.get('username') password = request.POST.get('password') if user == 'yjp' and password == 'abc':#登陸驗證成功 obj = redirect('/index') obj.set_cookie('cookie_id','cookie_value')#設置cookie,在瀏覽器存儲{'cookie_id':'cookie_value'} return obj return render(request, 'login.html', locals())
2:獲取cook
def index(request): co =request.COOKIES.get('cookie_id',None)#根據cooke_id獲取cookie if co : return render(request, 'index.html')#存在cookie跳轉首頁 return render(request, 'login.html')#不存在 跳轉登陸頁
瀏覽器第一次請求,攜帶一個空的cookie,服務器相應的時候,往空的cookie,添加信息。以后的每次請求都攜帶該cookie.
3:刪除cookie
response.delete_cookie("cookie_key",path="/",domain=name)
三:session
3.1:什么是session
session和cookie的作用一樣相似,用來保存用戶的狀態信息。通常保存在服務器上,對用戶而言,不可見,相對安全。
保存方式:使用鍵值對的方式保存,{key:value,key:value}
優點:相對於cookie較為安全。
缺點:通常保存在數據庫上,每次保存數據都會進行數據庫的讀取或寫入,影響效率。
3.2:session實現
def log_in2(request):
if request.method=='POST': user = request.POST.get('username') password = request.POST.get('password') if user == 'yjp' and password == 'abc':print('session',request.session) session=request.session session['verfiy'] ='is_login' #設置cookie obj = redirect('/index') return obj return render(request, 'login.html', locals()) def index2(request): co = request.session.get('verfiy', None)#獲取cookie if co == 'is_login': return render(request, 'index.html') return render(request, 'login.html'
四:django登陸驗證,auth.利用cookie+session的保存技術
4.1:cookie+session保存過程
1:瀏覽器第一次發送請求的時候,發送一個空的cookie,服務器此時設置session 如{'user':'yuan'}。
2:當session設置成功后,服務器會做兩步操作。一:生成一個隨機字符串,並將該字符串當作當作session_key,{'user':'yuan'}當作session-data 存在數據庫上。
第二步,生成一個cookie{'session_id':'隨機字符串'},返回瀏覽器。
3:瀏覽器下次發送請求的時候cookie={'session_id':'隨機字符串'},服務器拿到隨機字符串,當作session_key,在數據庫中取出session_data,這樣就完成了session值的傳遞。
4.2:auth驗證的代碼實現
from django.shortcuts import render,redirect,HttpResponse import random from django.contrib import auth #導入登陸驗證的模塊 # Create your views here. from django.http import JsonResponse def index(request): if request.user: #判斷用戶是否存在 print(request.user) return render(request,'index.html') else: return render(request,'login.html') def login(request): if request.method=='GET': return render(request,'login.html') if request.method=='POST': data={'user':None,'msg':None} print(request.POST) user =request.POST['user'] pwd =request.POST['pwd'] code =request.POST['code'] if code.lower()==request.session['code'].lower(): user=auth.authenticate(username=user,password=pwd)#登陸驗證,驗證用戶登陸信息是否正確,正確返回一個user已經驗證通過的用戶 if user: auth.login(request,user)#接受一個request對象和通過登陸驗證user,並完成上面三步cookie+session的設置 data['user']=True else: data['msg'] = '用戶名或密碼錯誤' else: data['msg']='驗證碼錯誤' return JsonResponse(data)
def logout_view(request):
auth.logout(request)#注銷用戶
五:User對象:
User對象有兩個屬性(user,password)。可以通過request.user.is_authenticated()來驗證用戶登陸是否通過驗證,通過驗證返回true.
驗證后的怎么操作和該方法無關。該方法只做驗證這一個功能。