[英]Python-falcon handling session
我正在使用python flask構建一個簡單的Web應用程序,用戶可以在其中點擊localhost:8000/
並登錄。 如果登錄成功,則會顯示另一個頁面,但我想知道如果用戶已經登錄,我該如何重定向到主頁面? 例如,如果我第一次登錄,我將被帶到主頁面,如果我打開第二個選項卡並再次點擊URL進行登錄,我會自動重定向到主頁面(很像gmail?)。
class LoginPage(object):
def on_get(self, req, resp, form={}):
對於非常簡單的應用程序,HTTP Basic Auth可能已經足夠好了。 Flask使這很容易。 以下裝飾器應用於僅對某些用戶可用的函數:
from functools import wraps
from flask import request, Response
def check_auth(username, password):
"""This function is called to check if a username password combination is valid. """
return username == 'admin' and password == 'secret'
def authenticate():
"""Sends a 401 response that enables basic auth"""
return Response(
'Could not verify your access level for that URL.\n'
'You have to login with proper credentials', 401,
{'WWW-Authenticate': 'Basic realm="Login Required"'})
def requires_auth(f):
@wraps(f)
def decorated(*args, **kwargs):
auth = request.authorization
if not auth or not check_auth(auth.username, auth.password):
return authenticate()
return f(*args, **kwargs)
return decorated
要使用此裝飾器,只需包裝一個視圖功能:
@app.route('/secret-page')
@requires_auth
def secret_page():
return render_template('secret_page.html')
如果您使用帶有mod_wsgi的基本身份驗證,則必須啟用身份驗證轉發,否則apache將使用所需的頭並且不會將其發送到您的應用程序: WSGIPassAuthorization 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.