flask鏈夊叧璁よ瘉鐨勬墿灞?/h4> 鎵╁睍 璇存槑 flask-login 绠$悊宸茬櫥褰曠敤鎴风殑鐢ㄦ埛浼氳瘽 werkzeug 璁$畻瀵嗙爜鏁e垪鍊煎苟杩涜鏍稿 itsdangerous 鐢熸垚骞舵牳瀵瑰姞瀵嗗畨鍏ㄤ护鐗?/td> flask-mail 鍙戦€佷笌璁よ瘉鐩稿叧鐨勯偖浠?/td> werkzeug璁$畻瀵嗙爜鏁e垪鍊?/h4>
鍦ㄦ暟鎹簱涓繚瀛樼敤鎴峰瘑鐮佹椂,澶勪簬瀹夊叏鑰冭檻,鎴戜滑鍌ㄥ瓨鐨勭敤鎴峰瘑鐮佸姞瀵嗗悗鐨勫瘑鐮佹暎鍒楀€?
werkzeug涓殑security妯″潡涓彁渚涚殑涓や釜鏂规硶鍙互寰堟柟渚跨殑瀹炵幇杩欎釜鍔熻兘.
generate_password_hash(password, method=pdkdf2:sha1, salt_length=8), 璇ユ柟娉曡緭鍏ヤ负鍘熷瀵嗙爜,杈撳嚭鏄瓧绗︿覆褰㈠紡鐨勫瘑鐮佹暎鍒楀€?鍙互鐢ㄥ湪鐢ㄦ埛娉ㄥ唽鐨勬椂鍊?鏁版嵁搴撲腑淇濆瓨鐢ㄦ埛瀵嗙爜鏁e垪鍊?method, salt_length鐨勯粯璁ゅ€煎氨鑳芥弧瓒冲ぇ閮ㄥ垎闇€姹? check_password_hash(hash, password),杩欎釜鍑芥暟鐨勫弬鏁版槸鏁版嵁搴撲腑瀛樺偍鐨勫瘑鐮佹暎鍒楀€煎拰鐢ㄦ埛鎻愪緵鐨勫瘑鐮?涓よ€呰繘琛屾牳瀵?濡傛灉瀵嗙爜姝g‘鍒欒繑鍥濼rue,鍚﹀垯,杩斿洖False. flask-login璁よ瘉鐢ㄦ埛 flask-ligin浣跨敤鏂规硶 瑕佹兂浣跨敤flask-login鎵╁睍,user妯″瀷涓繀椤昏瀹炵幇鍥涗釜鏂规硶: 鏂规硶 璇存槑 is_authenticated() 濡傛灉鐢ㄦ埛鐧诲綍,蹇呴』杩斿洖True, 鍚﹀垯杩斿洖False is_active() 濡傛灉鍏佽鐢ㄦ埛鐧诲綍,蹇呴』杩斿洖True,鍚﹀垯杩斿洖False,濡傛灉瑕佺鐢ㄨ处鎴?鍙互杩斿洖False is_anonymous() 瀵规櫘閫氱敤鎴峰繀椤昏繑鍥濬alse get_id() 蹇呴』杩斿洖鐢ㄦ埛涓€涓爣璇嗙,浣跨敤Unicode 缂栫爜鐨勫瓧绗︿覆杩欏洓涓柟娉曞彲浠ュ湪User妯″瀷涓疄鐜?涔熷彲浠ョ户鎵縡lask-login涓殑UserMixin绫绘潵瀹炵幇,UserMixin绫讳腑鍖呭惈浜嗕互涓婂洓绉嶆柟娉曠殑榛樿瀹炵幇,鑳藉婊¤冻澶у鏁?br>闇€姹? 鍦║serMixin绫讳腑,杩欏洓绉嶆柟娉曢兘娣诲姞浜咢property瑁呴グ鍣?灏嗘柟娉曡浆鍖栨垚浜嗗睘鎬?渚嬪:user.is_authenticated鑰屼笉鐢╱ser.is_authenticated()
flask-login瑕佹眰绋嬪簭瀹炵幇涓€涓洖璋冨嚱鏁?浣跨敤鎸囧畾鐨勬爣璇嗙鍔犺浇鐢ㄦ埛.鍑芥暟鐨勫疄鐜板涓?
from . import login_manager @login_manager.user_loader def load_user(user_id): return User.query.ger(int(user_id))
鍔犺浇鐢ㄦ埛鐨勫洖璋冨嚱鏁版帴鍙椾互Unicode瀛楃涓插舰寮忚〃绀虹殑鐢ㄦ埛鏍囪瘑绗?濡傛灉鑳芥壘鍒扮敤鎴?鍒欒繑鍥炵敤鎴峰璞?鍚﹀垯杩斿洖None.
flask-login 鍦ㄥ伐鍘傚嚱鏁颁腑鐨勫垵濮嬪寲鏂规硶:
from flask import Flask from flask.ext.login import LoginManager # 鍒涘缓LoginManager瀹炰緥瀵硅薄 login_manager = LoginManager() # 璁剧疆鐢ㄦ埛浼氳瘽淇濇姢绛夌骇.strong涓鸿褰曞鎴风ip鍜屾祻瑙堝櫒鐢ㄦ埛浠g悊淇℃伅,濡傚彂鐜板紓甯稿氨閫€鍑虹櫥褰?login_manager.session_protection = 'strong' # 璁剧疆鐧诲綍椤甸潰鐨勭鐐?'钃濇湰鍚?瑙嗗浘鍑芥暟鍚? login_manager.login_view = 'auth.login'
def create_app('config_name'): app = Flask() app.config.from_object('config_name') login_manager.init_app(app) # 娉ㄥ唽钃濆浘 ... return appflask-login妯″潡鐩稿叧鏂规硶 login_user(user, remenber=False, force=False, fresh=True)
褰撶敤鎴风櫥褰曚互鍚庯紝濡傛灉闇€瑕佽褰曠敤鎴风殑鐘舵€佸垯鍒欏彲浠ヨ皟鐢?login_user鍑芥暟璁板綍褰撳墠鐧诲綍鐨勭敤鎴枫€?/p>
login_user涓昏鐨勪綔鐢ㄥ氨鏄皢user鐨刬d鍔犲叆鍒皊ession涓€俽emember鍙傛暟鏄叧闂祻瑙堝櫒鏄惁闇€瑕侀噸鏂扮櫥褰?
褰撶敤鎴疯闂湭鎺堟潈鐨刄RL鏃朵細鏄剧ず鐧诲綍琛ㄥ崟,Flask-Login浼氭妸鍘熷湴鍧€淇濆瓨鍦ㄦ煡璇㈠瓧绗︿覆鐨刵ext鍙傛暟涓?杩欎釜鍙傛暟鍙粠request.args瀛楀吀涓鍙?
logout_user()logout_user()鍑芥暟,鍒犻櫎骞堕噸璁剧敤鎴蜂細璇?
褰撲腑鏌愪簺鎿嶄綔闇€瑕佺敤鎴风櫥褰曠殑鏃跺€欙紝灏遍渶瑕佺敤鍒拌楗板櫒login_required銆?/p>
@main.route('/post') @login_required def post(): pass
current_user璁板綍浜嗗綋鍓嶇櫥褰曪紙璁よ瘉锛夌敤鎴风殑淇℃伅锛屾彁渚涗簡涓€涓闂拰琛ㄧず褰撳墠鐧诲綍鐨勫璞°€?/p>
current_user瀵圭敤鎴峰疄渚嬭繘琛屼簡灏佽,濡傛灉鎯宠幏寰楃湡姝g殑鐢ㄦ埛瀵硅薄,鍙互閫氳繃current_user._get_current_object()鑾峰彇.
浣跨敤itsdangerous鐢熸垚纭浠ょ墝itsdangerous鎻愪緵浜嗗绉嶄护鐗岀敓鎴愮殑鏂规硶,鍏朵腑TimedJSONWebSignatureSerializer绫诲彲浠ョ敓鎴愪竴涓叿鏈夎繃鏈熸椂闂寸殑JSON WEB绛惧悕.杩欎釜绫荤殑鏋勯€犲嚱鏁扮殑鍙傛暟鏄竴涓瘑閽?
璇ョ被鎻愪緵浜哾umps()鍜宭oads()鏂规硶:
dumps()鏂规硶涓烘寚瀹氱殑鏁版嵁鐢熸垚涓€涓姞瀵嗙鍚?鐒跺悗瀵规暟鎹拰绛惧悕杩涜搴忓垪鍖?鐢熸垚浠ょ墝瀛楃涓?璇ユ柟娉曞寘鍚袱涓弬鏁?绗竴涓槸瑕佸姞瀵嗙殑鏁版嵁,绗簩涓槸杩囨湡鏃堕棿expires_in.鍗曚綅鏄.
loads()鏂规硶,鍏跺敮涓€鐨勫弬鏁版槸浠ょ墝瀛楃涓?璧蜂綔鐢ㄦ槸,灏嗗姞瀵嗙殑浠ょ墝瀛楃涓茶浆鍖栦负鍘熷瀛楃涓?鍏朵腑鍙互妫€娴嬬鍚嶅強杩囨湡鏃堕棿.濡傛灉浠ょ墝涓嶆纭垨鑰呰繃鏈?鍒欐姏鍑哄紓甯?
纭鐢ㄦ埛璐︽埛鐨勭ず渚嬪涓?
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer from flask import current_app Class User(db.Model): ... def generate_confirm_token(self, expiration=3600): s = Serializer('secret key', expiration) return s.dumps({'confirm': self.id) def confirm(self, token) s = Serializer('secret key') try: data = s.loads(token) except: return False if data.get('confirm') != self.id: return False self.confirmed = True # 鎻愪氦鏁版嵁搴? db.session.add(self) db.session测试数据mit() return Trueflask-mail鍙戦€侀偖浠?/h4>
鍦ㄧ敤鎴风‘璁ょ郴缁熶腑,鍙互鍒╃敤flask-mail缁欐敞鍐岀殑鐢ㄦ埛鍙戦€佺‘璁ら偖浠?鍙湁鍦ㄧ敤鎴风‘璁ゅ悗,鎵嶇畻鏄湡姝g殑娉ㄥ唽瀹屾垚.
鍒濆鍖杅lask-mail(鍦ㄥ嚱鏁板伐鍘備腑鍒濆鍖?from flask import Flask from flask.ext.mail import Mail mail = Mail() def create_app('config_name'): app = Flask(__name__) ... mail.init_app(app)閰嶇疆
flask-mail 杩炴帴鍒扮畝鍗曠殑閭欢浼犺緭鍗忚(Simple Mail Transfer Protocol, SMTP)鏈嶅姟鍣?鎶婇偖浠朵氦涓繖涓湇鍔″櫒鍙戦€?
鍦ㄥ彂閫侀偖浠朵箣鍓?蹇呴』閰嶇疆SMTP鏈嶅姟鍣?閰嶇疆鍙傛暟濡備笅(浠?63涓轰緥):
MAIL_SERVER = SMTP.163测试数据 # 鏈嶅姟鍣ㄤ富鏈?涓€鑸琒MTP.xxx测试数据褰㈠紡,姣斿SMTP.qq测试数据 MAIL_PORT = 465 # 163鏈嶅姟鍣ㄧ鍙?SSL鍚敤绔彛涓€鑸槸465,涓嶅惎鐢ㄧ鍙f槸25 # MAIL_USE_TLS = 25 # 鍚敤浼犺緭灞傚畨鍏ㄥ崗璁?Transport Secrity) MAIL_USE_SSL = 465 # 鍚敤瀹夊叏濂楅樁灞傚崗璁?Security Socket) MAIL_USERNAME = xxx.163测试数据 # 閭璐︽埛鐢ㄦ埛鍚?MAIL_PASSWORD = xxxxxx # 163瀹㈡埛绔巿鏉冪爜, 涓嶆槸鐧诲綍瀵嗙爜, 鎺堟潈鐮佸彲浠ュ湪閭璁剧疆閲屾煡鎵?/pre> 鍙戦€侀偖浠?/h5>from flask.ext.mail import Message from . import mail # subject鏄偖浠剁殑涓婚, sender鏄彂閫佹柟閭,recipients鏄帴鏀舵柟閭鍒楄〃, msg.body鏄偖浠舵鏂?msg.body鏄偖浠剁殑html姝f枃 msg = Message(subject, sender=xxx, recipients=[xxx]) msg.body = 'test body' msg.html = '<b>html</b> body' # 鍦╢lask涓?flask-mail涓殑mail()鍑芥暟浣跨敤鐨刢urrent_app,鍦╯hell鐜涓繍琛?蹇呴』婵€娲荤殑绋嬪簭涓婁笅鏂囦腑鎵ц with app.app_context(): mail.send(msg)寮傛鍙戦€侀偖浠?/h5>寮傛鍙戦€佺數瀛愰偖浠跺彲浠ュ皢鍙戦€佺數瀛愰偖浠剁殑绋嬪簭绉诲埌鍚庡彴绾跨▼涓紱浠g爜濡備笅
from flask.ext.mail import Message from . import mail from threading import Thread def send_async_email(app, msg) with app.app_context(): mail.send(msg) def send_email(subject, sender, recipients): msg = Message(subject, sender, recipients) msg.body = 'test body' msg.html = '<b>html</b> body' thr = Thread(target=send_async_email, args=[app, msg]) thr.start() return thr寰堝flask鎵╁睍閮藉亣璁惧凡缁忓瓨鍦ㄦ縺娲荤殑绋嬪簭涓婁笅鏂囧拰璇锋眰涓婁笅鏂?flask-mail涓殑send()鍑芥暟浣跨敤current_app, 鍥犳蹇呴』鏈夋縺娲荤殑绋嬪簭涓婁笅鏂?鍦ㄤ笉鍚岀嚎绋嬩腑鎵цmail.send()鍑芥暟鏃?绋嬪簭涓婁笅鏂囪鐢╝pp.app_context()浜哄伐鍒涘缓.
琛ュ厖绋嬪簭鍦ㄥ彂閫佸ぇ閲忕殑鐢靛瓙閭欢鏃?浣跨敤涓撻棬鍙戦€佺數瀛愰偖浠朵綔涓氳姣旀瘡灏佺數瀛愰偖浠堕兘鏂板缓涓€涓嚎绋嬭鏇村悎閫?渚嬪鍙互鍚ф墽琛宻end_async_email()鍑芥暟鐨勬搷浣滃彂缁機elery(http://HdhCmsTestceleryproject.org/)浠诲姟闃熷垪.鍏充簬鍏蜂綋鎿嶄綔,鍚庣画缁х画瀛︿範涓?/a>.
鍘熸枃:澶т笓鏍?/a> flask-鐢ㄦ埛璁よ瘉绯荤粺
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did171087