日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

登录页面html代码_【网络自动化平台开发】—— 登录组件

發布時間:2025/3/15 编程问答 12 豆豆
生活随笔 收集整理的這篇文章主要介紹了 登录页面html代码_【网络自动化平台开发】—— 登录组件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

今年的天氣特別的冷,于是,繼續緩慢更新。

Django 其實有自己的權限認證組件,可以拿來用,其實本身代碼量也不多,還是自己寫吧。

新建了一個functions文件夾用于存放各種功能文件:

說說登錄的原理吧,企業中一般使用AD作為認證源,微軟域服務本身只有Windows系統可用,但非Windows可以使用ldap協議來進行認證,這里需要用到的庫ladp3,在functions文件夾中新建publicldaplogin.py文件。功能代碼如下:

from ldap3 import Server,Connection,SIMPLE,SYNC,ALL,SASL,NTLM,SUBTREE,ALL_ATTRIBUTES,ALL_OPERATIONAL_ATTRIBUTES import times = Server('10.210.12.17',get_info=ALL)def login(username,password):c = Connection(s, user='head.yst%s'%username, password=password, authentication=NTLM)if not c.bind():result = {'result':'False'}return resultelse:c.search(search_base='dc=head,dc=yst',search_filter='(anr={})'.format(username),search_scope=SUBTREE,attributes=['displayName','title'],# attributes=ALL_ATTRIBUTES)result = {'result':'Success','username':username,'fullname':str(c.entries[0]).split('n')[1].split(':')[1].lstrip(),'logintime':time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))}return result

代碼末尾加上print(login('xcwang','password123'))我們來運行腳本,xcwang和password123對應的正是ad的用戶名密碼,得到以下結果:

{'result': 'Success', 'username': 'xcwang', 'fullname': '王小川', 'logintime': '2020-10-30 10:00:08'}

功能測試正常,刪除最后一行,登錄功能組件就寫完了,

但是需要在平臺中使用起來,還需要一些工作,登錄腳本只是滿足了去驗證用戶身份的功能,用戶成功登錄后,我們應該設置一個令牌給到用戶,拿到令牌的用戶,可以在系統中進行各種被允許的操作。

所以,我們需要一個給用戶發令牌的功能,functions中新建public_token_auth.py

import time import jwt#用戶通過此腳本獲取到token def get_token(username, key):token_dict={'time':time.time(),'name':username}headers ={'alg':"HS256"}jwt_token=jwt.encode(token_dict,key,algorithm="HS256",headers=headers).decode()return jwt_token#對用戶的token進行認證,確保是我們之前發出去的 def dec_token(token_key,key):try:data = jwt.decode(token_key, key , algorithms=['HS256'])['name']return dataexcept Exception as e:return e

驗證token功能呢,如下:

末尾加上print(get_token('abc','123')),然后運行 eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0aW1lIjoxNjA0MDI0NjkzLjQzNDE2NiwibmFtZSI6ImFiYyJ9.0GSfXloR9Y7O9OD3wD-33ErotX58U13_dCOJ7BcMe_8將末尾的print改成print(dec_token('eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0aW1lIjoxNjA0MDI0NjkzLjQzNDE2NiwibmFtZSI6ImFiYyJ9.0GSfXloR9Y7O9OD3wD-33ErotX58U13_dCOJ7BcMe_8','123')) 再次運行,得到的結果為abc即可

驗證完成后記得刪除多余代碼,接著回到views.py,

from django.shortcuts import render,redirect from functions import public_ldap_login,public_token_authdef login(request):#token的獲取使用key'abcd.com.cn'if request.method == 'POST':#獲取用戶POST上來的用戶名和密碼un = request.POST.get('username')pd = request.POST.get('password')#判斷用戶名密碼為非空,如果為空,則返回消息和login頁面if un == '' or pd == '':res = render(request, 'login.html', context={'result':'用戶名密碼不能為空!'})return res#獲取登錄結果login_result = public_ldap_login.login(un,pd)#如果登錄結果為false則返回頁面和登錄失敗消息。if login_result['result'] == 'False':res = render(request, 'login.html', context={'result':'登陸失敗,用戶名密碼錯誤,請檢查!'})return res#如果登錄成功則執行以下操作else:#如果session key中包含'login_from‘字段,則表示用戶是從其它頁面跳轉到login的if 'login_from' in request.session.keys():#則為用戶準備以下資源:1、跳轉到login_from定義的頁面,設置token,username,fullname,logintime的cookieres = redirect(request.session['login_from'])res.set_cookie('token',public_token_auth.get_token(un,'abcd.com.cn'),max_age=36000)res.set_cookie('username', un,max_age=36000)res.set_cookie('fullname',login_result['fullname'],max_age=36000)res.set_cookie('logintime',login_result['logintime'],max_age=36000)res.set_cookie('result','Success',max_age=36000)return res#如果session ky中不包含login_from字段,則直接返回login.html,并要求重新登錄else:return render(request,'login.html')#如果使用非POST非GET的請求方法,則直接返回login頁面,要求強制登錄else:return render(request,'login.html')#下面定義個裝飾器函數,一方面,實現登錄邏輯,另外其它頁面函數也方便調用。 def login_auth(fouc):def wrapper(request,*args,**kwargs):cookie_get = request.COOKIES #獲取用戶cookieuser_path = request.path #獲取用戶訪問的path,非常重要!!#如果未曾認證,或cookie不完整,則直接重定向到login頁面if 'token' not in cookie_get or 'username' not in cookie_get or cookie_get['username'] != public_token_auth.dec_token(cookie_get['token'],'abcd.com.cn') and user_path != '/login/': #設置request.session['login_from']為user_path!!request.session['login_from'] = user_path return redirect('/login/') # 此時用戶的request.session中已經包含了user_path,登錄成功后,可直接跳轉至登錄之前的頁面#如果用戶跳轉到login頁面之前的頁面就是/login/,則直接返回login.htmlelif user_path == '/login/':request.session['login_from'] = '/'return render(request,'login.html')else:return fouc(request,*args,**kwargs)return wrapper@login_auth def index(request):return render(request,'index.html')@login_auth def infosearch(request):return render(request,'infosearch.html')

結果驗證:

打開http://127.0.0.1,將會被重定向至login頁面,輸入正確的ad用戶名密碼后,將直接跳轉至主頁,輸入錯誤的用戶名密碼或不輸入直接點擊登錄,會有相應的錯誤提示。

刪除cookie直接嘗試訪問http://127.0.0.1/infosearch 會被重定向至登錄頁面,輸入正確的用戶名密碼后,自動跳轉至infosearch頁面。

再來梳理一下,登錄組件邏輯:

1、嘗試獲取用戶cookie和訪問路徑;
2、如果用戶未認證,則重定向到login函數進行處理(處理步驟在下面);
3、如果用戶的訪問路徑為‘/login’,則直接返回login.html頁面
4、如果2、3都沒有匹配到,則正常執行用戶的請求。

login函數處理用戶請求邏輯:
如果用戶是通過POST發送登錄信息,則處理用戶登錄,并將結果寫入到用戶cookie;
其它情況一律返回login.html,要求登錄。

今天先到這里,后面將繼續更新。

總結

以上是生活随笔為你收集整理的登录页面html代码_【网络自动化平台开发】—— 登录组件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。