Kar*_*cek 2 python authentication azure password-protection bokeh
我有一个简单的bokeh服务器应用程序,我想在基于Linux的Azure节点上公开它。服务器在那里并且正在运行。
我的问题是:如何通过用户名和密码保护内容?我不一定需要用户身份验证。
到目前为止,我的想法(未尝试过,可能行不通)
您可以尝试仅通过用户身份验证来禁用bokeh服务器生成会话ID并仅由外部应用程序生成会话ID :(
基于bokeh文档的这一部分)
bokeh secret命令生成密钥:$散景秘密 oIWDL7DVYCaBJG9eYQ2Wvf2f2uhOAIM8xNS8Kds3eizV
BOKEH_SECRET_KEY环境变量设置为生成值;$ export BOKEH_SECRET_KEY = oIWDL7DVYCaBJG9eYQ2Wvf2f2uhOAIM8xNS8Kds3eizV
$ export BOKEH_SIGN_SESSIONS =真实
--session-ids external-signed参数运行bokeh服务器:$ bokeh服务myApp --session-ids外部签名
在此模式下,用户应提供有效(签名)的会话ID来访问bokeh服务器。
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)
| 归档时间: |
|
| 查看次数: |
2721 次 |
| 最近记录: |