您可以尝试禁用散景服务器生成会话 ID,并仅在用户身份验证后由外部应用程序生成它们:
(基于这部分散景文档)
- 生成密钥
bokeh secret
命令:
$ bokeh secret
oIWDL7DVYCaBJG9eYQ2Wvf2f2uhOAIM8xNS8Kds3eizV
- Set
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。
这是基于的示例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()
- 现在要访问 bokeh 服务器,用户应该转到 Flask 应用程序并指定登录名和密码。