散景服务器的简单用户名和密码保护
Simple username & password protection of a bokeh server
我有一个简单的散景服务器应用程序,我想在基于 Linux 的 Azure 节点上公开它。服务器在那里 运行。
我的问题是:如何通过用户名和密码保护内容?我不一定需要用户身份验证。
我目前的想法(没试过,可能行不通)
- 创建一个带有文本字段的额外散景服务器页面。
- 在按钮的回调中,添加密码是否合适的测试。如果是,则重定向到原始服务器页面。否则,通知用户凭据错误。
您可以尝试禁用 bokeh 服务器生成会话 ID,并仅在用户身份验证后由外部应用程序生成它们:
(基于散景文档的 this part)
- 使用
bokeh secret
命令生成密钥:
$ bokeh secret
oIWDL7DVYCaBJG9eYQ2Wvf2f2uhOAIM8xNS8Kds3eizV
- 将
BOKEH_SECRET_KEY
环境变量设置为生成的值;
$ export BOKEH_SECRET_KEY=oIWDL7DVYCaBJG9eYQ2Wvf2f2uhOAIM8xNS8Kds3eizV
- 设置另一个环境变量:
$ export BOKEH_SIGN_SESSIONS=True
- 运行 带有
--session-ids external-signed
参数的散景服务器:
$ bokeh serve myApp --session-ids external-signed
在此模式下,用户应提供有效(已签名)会话 ID 才能访问散景服务器。
- 运行 简单的外部过程,要求用户输入登录名和密码并为他们生成 ID。
这是基于 Flask 文档中 snippet 的示例:
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()
- 现在要访问散景服务器,用户应该转到 Flask 应用程序并指定登录名和密码。
我有一个简单的散景服务器应用程序,我想在基于 Linux 的 Azure 节点上公开它。服务器在那里 运行。
我的问题是:如何通过用户名和密码保护内容?我不一定需要用户身份验证。
我目前的想法(没试过,可能行不通)
- 创建一个带有文本字段的额外散景服务器页面。
- 在按钮的回调中,添加密码是否合适的测试。如果是,则重定向到原始服务器页面。否则,通知用户凭据错误。
您可以尝试禁用 bokeh 服务器生成会话 ID,并仅在用户身份验证后由外部应用程序生成它们:
(基于散景文档的 this part)
- 使用
bokeh secret
命令生成密钥:
$ bokeh secret oIWDL7DVYCaBJG9eYQ2Wvf2f2uhOAIM8xNS8Kds3eizV
- 将
BOKEH_SECRET_KEY
环境变量设置为生成的值;
$ export BOKEH_SECRET_KEY=oIWDL7DVYCaBJG9eYQ2Wvf2f2uhOAIM8xNS8Kds3eizV
- 设置另一个环境变量:
$ export BOKEH_SIGN_SESSIONS=True
- 运行 带有
--session-ids external-signed
参数的散景服务器:
$ bokeh serve myApp --session-ids external-signed
在此模式下,用户应提供有效(已签名)会话 ID 才能访问散景服务器。
- 运行 简单的外部过程,要求用户输入登录名和密码并为他们生成 ID。 这是基于 Flask 文档中 snippet 的示例:
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()
- 现在要访问散景服务器,用户应该转到 Flask 应用程序并指定登录名和密码。