django中賬號(hào)密碼驗(yàn)證登陸功能的實(shí)現(xiàn)方法
今天分享一下django的賬號(hào)密碼登陸,前端發(fā)送ajax請(qǐng)求,將用戶名和密碼信息發(fā)送到后端處理,后端將前端發(fā)送過來的數(shù)據(jù)跟數(shù)據(jù)庫進(jìn)行過濾匹配,成功就跳轉(zhuǎn)指定頁面,否則就把相對(duì)應(yīng)的錯(cuò)誤信息返回,同時(shí)增加一個(gè)小功能,在規(guī)定時(shí)間內(nèi)超過規(guī)定的登錄次數(shù),就鎖住無法登陸,等下一個(gè)時(shí)間段再允許登陸。
一、通過ORM創(chuàng)建一張歷史登陸表
class login_history(models.Model): user = models.CharField(max_length=32, verbose_name='登錄用戶') ip = models.GenericIPAddressField(verbose_name='用戶IP地址') count = models.SmallIntegerField(verbose_name='登錄次數(shù)') lock = models.SmallIntegerField(verbose_name='鎖') utime = models.DateTimeField(auto_now=True, verbose_name='更新時(shí)間') class Meta: db_table = "login_history" verbose_name = "歷史登錄" verbose_name_plural = "歷史登錄" def __str__(self): return self.user
二、前端圖片跟代碼展示
前端代碼
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title> - 登錄</title> <meta name="keywords" content=""> <meta name="description" content=""> <link rel="shortcut icon" href="/static/favicon.ico"> {% include 'css_comm.html' %} </head> <body class="gray-bg"> <div class="middle-box text-center loginscreen animated fadeInDown" > <div style="margin-top: 40px"> <h3>歡迎使用 AutoOps</h3> {% csrf_token %} <div class="form-group" > <input type="text" name="user" value="" class="form-control user_obj" placeholder="用戶名" required="" autocomplete="off"> </div> <span style="display: block;margin-bottom: 10px;color:red" id="user_error"></span> <div class="form-group"> <input type="password" name="pwd" value="" class="form-control pwd_obj" placeholder="密碼" required="" autocomplete="off"> </div> <span style="display: block;margin-bottom: 10px;color:red" id="pwd_error"></span> <button type="submit" class="btn btn-info block full-width m-b btn_obj">登 錄</button> 記住密碼 <input type="checkbox" style="margin: 0"> </div> </div> {% include 'js_comm.html' %} <script> $(".btn_obj").click(function () { var user = $(".user_obj").val(); var pwd = $(".pwd_obj").val(); $.ajaxSetup({headers: {"X-CSRFToken": '{{ csrf_token }}'}}); $.ajax({ "url": "/login.html", "type": "post", "data": {"user":user,"pwd":pwd}, success: function (data) { var obj = JSON.parse(data); if (obj.status) { $(".user_obj").css("border-color",""); $("#user_error").text(""); $(".pwd_obj").css("border-color",""); $("#pwd_error").text(""); window.location.href = '/assets/assets_list.html'; } else { if (obj.error.user_error != "") { $(".user_obj").css("border-color","red"); $("#user_error").text(obj.error.user_error); }else { $(".user_obj").css("border-color",""); $("#user_error").text(""); } if (obj.error.pwd_error != "") { $(".pwd_obj").css("border-color","red"); $("#pwd_error").text(obj.error.pwd_error); }else { $(".pwd_obj").css("border-color",""); $("#pwd_error").text(""); } if (obj.error.login_error != "") { alert("登錄失敗",obj.error.login_error, "", {type: 'error'}) } } } }) }) </script> </body> </html>
后端代碼
def login(request): ret = {"status": False, "error": {"user_error": "", "pwd_error": "", "login_error": ""}} if request.method == "POST": user = request.POST.get("user") #獲取用戶名 pwd = request.POST.get("pwd") #獲取密碼 if request.META['REMOTE_ADDR']: #判斷是否獲取用戶IP地址 access_ip = request.META['REMOTE_ADDR'] #存到access_ip變量中 else: access_ip = request.META['HTTP_X_FORWARDED_FOR'] #獲取用戶的真實(shí)IP,非代理IP if access_ip: ip_obj = models.login_history.objects.filter(ip=access_ip).first() #在歷史登錄表中查找是否有這個(gè)IP if ip_obj: current_time = datetime.datetime.now() #獲取當(dāng)前時(shí)間 second = current_time - ip_obj.utime #用當(dāng)前時(shí)間減去最近登錄的時(shí)間 second = second.seconds #轉(zhuǎn)換為秒數(shù) count = ip_obj.count #獲取當(dāng)前對(duì)象的登錄次數(shù) count = count + 1 #次數(shù)加1 ip_obj.count = count #修改次數(shù)信息 ip_obj.save() #保存 if second < 60 and count >= 10: #判斷秒數(shù)是否小于60秒并且次數(shù)大于等于10 ret["error"]["login_error"] = "過于頻繁登錄,你已經(jīng)被鎖著,等一會(huì)60秒之后再登錄" ip_obj.user = user #登錄的用戶名 ip_obj.lock = 1 #值為1表示鎖著 ip_obj.save() #保存 return HttpResponse(json.dumps(ret)) #返回給前端 elif ip_obj.lock == 1 and second >= 60: #判斷l(xiāng)ock是否等于1和秒數(shù)大于60秒 ip_obj.lock = 0 #值為0表示解鎖 ip_obj.count = 1 #初始化登錄次數(shù) ip_obj.save() #保存 else: models.login_history.objects.create(user=user, ip=access_ip, count=1, lock=0) #沒有登錄過,就創(chuàng)建記錄 if user: account_obj = Account.objects.filter(username=user).first() #判斷這個(gè)用戶名是否存在 if not account_obj: ret["error"]["user_error"] = "用戶名錯(cuò)誤或者不存在" else: ret["error"]["user_error"] = "用戶名不能為空" if pwd == "": ret["error"]["pwd_error"] = "密碼不能為空" users = authenticate(username=user, password=pwd) #驗(yàn)證用戶名和密碼是否一樣 if users: request.session["user_id"] = users.pk #存儲(chǔ)到session會(huì)話中 initial_session(users, request) ret["status"] = True ip_obj.count = 1 # 登錄次數(shù)等于1 ip_obj.save() return HttpResponse(json.dumps(ret)) #返回前端 else: ret["error"]["pwd_error"] = "用戶名或密碼不正確" return HttpResponse(json.dumps(ret)) return render(request, "login.html")
三、測(cè)試效果
1.不輸入賬號(hào)密碼登錄,會(huì)提示錯(cuò)誤信息
2.輸入不存在的用戶
3.輸入錯(cuò)誤密碼
4.在60秒內(nèi)連續(xù)輸錯(cuò)10次密碼,可以讓惡意登錄的,鎖著無法再讓其登錄
5.查看數(shù)據(jù)庫表信息
6.最后演示登錄成功跳轉(zhuǎn)圖
總結(jié)
以上所述是小編給大家介紹的django中賬號(hào)密碼驗(yàn)證登陸功能的實(shí)現(xiàn)方法,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
相關(guān)文章
python3之模塊psutil系統(tǒng)性能信息使用
psutil是個(gè)跨平臺(tái)庫,能夠輕松實(shí)現(xiàn)獲取系統(tǒng)運(yùn)行的進(jìn)程和系統(tǒng)利用率,這篇文章主要介紹了python3之模塊psutil系統(tǒng)性能信息使用,感興趣的小伙伴們可以參考一下2018-05-05Python中識(shí)別圖片/滑塊驗(yàn)證碼準(zhǔn)確率極高的ddddocr庫詳解
驗(yàn)證碼的種類有很多,它是常用的一種反爬手段,包括:圖片驗(yàn)證碼,滑塊驗(yàn)證碼,等一些常見的驗(yàn)證碼場(chǎng)景。這里推薦一個(gè)簡(jiǎn)單實(shí)用的識(shí)別驗(yàn)證碼的庫?ddddocr?(帶帶弟弟ocr)庫,希望大家喜歡2023-02-02Python連接PostgreSQL數(shù)據(jù)庫的方法
大家應(yīng)該都有所了解,python可以操作多種數(shù)據(jù)庫,諸如SQLite、MySql、PostgreSQL等,這里不對(duì)所有的數(shù)據(jù)庫操作方法進(jìn)行贅述,只針對(duì)目前項(xiàng)目中用到的PostgreSQL做一下簡(jiǎn)單介紹,主要是Python連接PostgreSQL數(shù)據(jù)庫的方法。有需要的朋友們可以參考借鑒,下面來一起看看吧。2016-11-11pytorch多GPU并行運(yùn)算的實(shí)現(xiàn)
這篇文章主要介紹了pytorch多GPU并行運(yùn)算的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09Python實(shí)現(xiàn)批量修改文件時(shí)間屬性
我們有時(shí)候需要修改文件的“修改時(shí)間”?、?“訪問時(shí)間”,“創(chuàng)建時(shí)間”?,此時(shí)如果使用Python批量實(shí)現(xiàn)應(yīng)該會(huì)方便很多,下面小編就來為大家介紹一下具體實(shí)現(xiàn)方法吧2023-11-11python+matplotlib繪制簡(jiǎn)單的海豚(頂點(diǎn)和節(jié)點(diǎn)的操作)
這篇文章主要介紹了python+matplotlib繪制簡(jiǎn)單的海豚(頂點(diǎn)和節(jié)點(diǎn)的操作),具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01Python一鍵實(shí)現(xiàn)PDF文檔批量轉(zhuǎn)Word
無論是在工作還是學(xué)習(xí)當(dāng)中,大家都會(huì)遇到這樣一個(gè)問題,將“PDF當(dāng)中的內(nèi)容(文本和圖片)轉(zhuǎn)換為Word的格式”,本文將用Python實(shí)現(xiàn)一鍵批量將PDF轉(zhuǎn)Word,需要的可以參考一下2022-08-08python實(shí)現(xiàn)文件批量編碼轉(zhuǎn)換及注意事項(xiàng)
本文通過實(shí)例代碼給大家介紹了python實(shí)現(xiàn)文件批量編碼轉(zhuǎn)換及注意事項(xiàng),非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2019-10-10