Python 简单用户名&;bokeh服务器的密码保护
我有一个简单的bokeh服务器应用程序,我想在基于Linux的Azure节点上公开它。服务器已经启动并正在运行 我的问题是:如何通过用户名和密码保护内容?我不需要用户的身份验证 到目前为止我的想法(没有尝试过,可能行不通)Python 简单用户名&;bokeh服务器的密码保护,python,azure,authentication,password-protection,bokeh,Python,Azure,Authentication,Password Protection,Bokeh,我有一个简单的bokeh服务器应用程序,我想在基于Linux的Azure节点上公开它。服务器已经启动并正在运行 我的问题是:如何通过用户名和密码保护内容?我不需要用户的身份验证 到目前为止我的想法(没有尝试过,可能行不通) 创建带有文本字段的额外bokeh服务器页面 在按钮的回调上,如果密码合适,则添加测试。如果是,则重定向到原始服务器页面。否则,请通知用户错误的凭据 您可以尝试禁用bokeh服务器生成会话id,并仅在用户身份验证后通过外部应用程序生成会话id: (基于bokeh文件的格式) 使
您可以尝试禁用bokeh服务器生成会话id,并仅在用户身份验证后通过外部应用程序生成会话id:
(基于bokeh文件的格式)
bokeh secret
命令生成密钥:
从functools导入包装
来自烧瓶导入请求、响应、重定向、烧瓶
从bokeh.util导入会话\u id
app=烧瓶(名称)
def check_auth(用户名、密码):
返回用户名==“有效用户”和密码==“有效密码”
def authenticate():
“”“发送启用基本身份验证的401响应”“”
返回响应(
'无法验证您对该URL的访问级别。\n'
“您必须使用正确的凭据登录”,401,
{'WWW-Authenticate':'Basic realm=“Login Required”})
def需要_auth(f):
@包装(f)
def装饰(*args,**kwargs):
auth=request.authorization
如果未验证或未验证,请检查\u auth(auth.username,auth.password):
返回身份验证()
返回f(*args,**kwargs)
回报
@应用程序路径(“/”)
@需要授权
def将_重定向到_bokeh():
s_id=会话_id.generate_session_id()
返回重定向(“http://://?bokeh会话id={}”。格式(s_id),代码=302)
如果名称=“\uuuuu main\uuuuuuuu”:
app.run()
根据您在没有用户认证的情况下的安全需求,我不理解您在没有用户认证的情况下的安全场景,这很奇怪,请发布更多详细信息。我建议参考文档,了解Azure和AzureAD上的保护机制。 $ bokeh secret oIWDL7DVYCaBJG9eYQ2Wvf2f2uhOAIM8xNS8Kds3eizV $ export BOKEH_SECRET_KEY=oIWDL7DVYCaBJG9eYQ2Wvf2f2uhOAIM8xNS8Kds3eizV $ export BOKEH_SIGN_SESSIONS=True $ bokeh serve myApp --session-ids external-signed
from functools import wraps
from flask import request, Response, redirect, Flask
from bokeh.util import session_id
app = Flask(__name__)
def check_auth(username, password):
return username == 'valid_user' and password == 'valid_password'
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('/')
@requires_auth
def redirect_to_bokeh():
s_id = session_id.generate_session_id()
return redirect("http://<bokeh-server-addr>:<port>/?bokeh-session-id={}".format(s_id), code=302)
if __name__ == "__main__":
app.run()