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

3

我有一个简单的bokeh服务器应用程序,我想将其暴露在基于Linux的Azure节点上。 服务器已经启动并运行。

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

我目前的想法(尚未尝试,可能不起作用)

  1. 创建一个额外的bokeh服务器页面,并添加一个文本字段。
  2. 在按钮的回调中,添加测试以检查密码是否匹配。 如果匹配,则重定向到原始服务器页面。 否则,通知用户凭据错误。

根据您的安全需求,如果没有对用户进行身份验证,我无法理解您的安全场景,这很奇怪,请发布更多详细信息。并且我建议参考文档《Azure AD的身份验证方案》(https://learn.microsoft.com/en-us/azure/active-directory/develop/active-directory-authentication-scenarios) 以了解在Azure和AzureAD下的保护机制。 - Peter Pan
1个回答

6
你可以尝试禁用Bokeh服务器生成会话ID,只有在用户认证后才由外部应用程序生成它们: (基于Bokeh文档的this part
1. 使用bokeh secret命令生成密钥:
$ bokeh secret
oIWDL7DVYCaBJG9eYQ2Wvf2f2uhOAIM8xNS8Kds3eizV
2. 将BOKEH_SECRET_KEY环境变量设置为生成的值;
$ export BOKEH_SECRET_KEY=oIWDL7DVYCaBJG9eYQ2Wvf2f2uhOAIM8xNS8Kds3eizV
3. 设置另一个环境变量:
$ export BOKEH_SIGN_SESSIONS=True
4. 使用--session-ids external-signed参数运行Bokeh服务器:
$ bokeh serve myApp --session-ids external-signed
在这种模式下,用户需要提供有效(已签名)的会话 ID 才能访问 Bokeh 服务器。
5. 运行简单的外部进程,要求用户输入登录名和密码并为他们生成 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()    

现在要访问bokeh服务器,用户需要进入Flask应用程序并指定登录名和密码。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接