bokeh服务器的简单用户名和密码保护

Kar*_*cek 2 python authentication azure password-protection bokeh

我有一个简单的bokeh服务器应用程序,我想在基于Linux的Azure节点上公开它。服务器在那里并且正在运行。

我的问题是:如何通过用户名和密码保护内容?我不一定需要用户身份验证。

到目前为止,我的想法(未尝试过,可能行不通)

  1. 用文本字段创建一个额外的bokeh服务器页面。
  2. 在按钮的回调中,添加密码是否合适的测试。如果是这样,则重定向到原始服务器页面。否则,通知用户有关错误凭证的信息。

tim*_*mur 6

您可以尝试仅通过用户身份验证来禁用bokeh服务器生成会话ID并仅由外部应用程序生成会话ID :(
基于bokeh文档的这一部分

  1. 使用以下bokeh secret命令生成密钥:
$散景秘密
oIWDL7DVYCaBJG9eYQ2Wvf2f2uhOAIM8xNS8Kds3eizV
  1. BOKEH_SECRET_KEY环境变量设置为生成值;
$ export BOKEH_SECRET_KEY = oIWDL7DVYCaBJG9eYQ2Wvf2f2uhOAIM8xNS8Kds3eizV
  1. 设置另一个环境变量:
$ export BOKEH_SIGN_SESSIONS =真实
  1. 使用--session-ids external-signed参数运行bokeh服务器:
$ bokeh服务myApp --session-ids外部签名

在此模式下,用户应提供有效(签名)的会话ID来访问bokeh服务器。

  1. 运行简单的外部过程,要求用户输入登录名和密码,并为他们生成ID。这是基于Flask文档中片段的示例:


    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()    
Run Code Online (Sandbox Code Playgroud)
  1. 现在要访问bokeh服务器,用户应转到Flask应用程序并指定登录名和密码。