python - Bottle 中的基本身份验证
问题描述
如何在瓶子框架中执行基本身份验证?在烧瓶中我曾经:
def check( username, password ):
# This function is called to check if a username/password combination is valid
return username == 'nikos' and password == '******'
def authenticate():
# Sends a 401 response that enables basic auth
return Response( 'Credentials of a registered user required!', 401, {'WWW-Authenticate': 'Basic realm="User!"'} )
并称为:
auth = request.authorization
if not auth or not counters.check( auth.username, auth.password ):
return counters.authenticate()
我怎样才能在瓶子框架中实现同样的目标?
解决方案
正如这里所报道的,Bottle本身包含一个装饰器,使 Basic Auth 非常简单:
from bottle import auth_basic, request, route
def is_authenticated_user(user, password):
# You write this function. It must return
# True if user/password is authenticated, or False to deny access.
@route('/')
@auth_basic(is_authenticated_user)
def home():
return ['hooray, you are authenticated! your info is: {}'.format(request.auth)]
推荐阅读
- r - 将点添加到条形图
- python - 如何删除数据框行索引?
- java - 在集合之间切换值得吗?
- php - http身份验证基本不适用于uri中的参数
- r - 在 R 中使用动态规划计算斐波那契
- python - ImportError:无法从“请求”导入名称“帖子”(C:\Program Files\Python37\lib\site-packages\request\__init__.py)
- recursion - 如何获取 JSON 文档中所有属性的列表?
- javascript - 使用 HTML 和 Javascript 进行加/减操作
- python - 当变量存在时,为什么会出现 Flake8 F821 错误?
- javascript - 如何将外部 js jquery 脚本从 html 网站模板修复到 Angular 项目中