Authentication without SSO
Introduction
想要为您的Streamlit应用添加密码保护,但无法实现单点登录?我们为您提供了解决方案!本指南向您展示了两种简单的技术,使用Secrets管理为您的Streamlit应用添加基本身份验证。
警告
虽然这种技术增加了一定程度的安全性,但它无法与使用SSO提供商的适当身份验证相提并论。
Option 1: One global password for all users
这是最简单的选项!您的应用程序将要求一个所有用户共享的密码。它将使用Secrets management存储在应用程序的秘密中。如果您想更改此密码或撤销用户的访问权限,您需要为所有人更改它。如果您希望每个用户都有一个单独的密码,请跳转到下面的选项2。
Step 1: Add the password to your local app secrets
您的本地 Streamlit 应用程序将从应用程序根目录中的文件 .streamlit/secrets.toml 读取密钥。如果此文件尚不存在,请创建此文件并按照以下方式添加您的密码:
# .streamlit/secrets.toml
password = "streamlit123"
重要
请确保将此文件添加到您的.gitignore中,以免提交您的秘密!
Step 2: Copy your app secrets to the cloud
由于上面的secrets.toml文件没有提交到GitHub,你需要将其内容单独传递给你部署的应用程序(在Streamlit社区云上)。转到应用程序仪表板,在应用程序的下拉菜单中,点击编辑Secrets。将secrets.toml的内容复制到文本区域。更多信息可在Secrets管理中找到。

Step 3: Ask for the password in your Streamlit app
将下面的代码复制到您的Streamlit应用程序中,在底部的check_password()函数调用下方插入您的正常应用程序代码,然后运行它:
# streamlit_app.py
import hmac
import streamlit as st
def check_password():
"""Returns `True` if the user had the correct password."""
def password_entered():
"""Checks whether a password entered by the user is correct."""
if hmac.compare_digest(st.session_state["password"], st.secrets["password"]):
st.session_state["password_correct"] = True
del st.session_state["password"] # Don't store the password.
else:
st.session_state["password_correct"] = False
# Return True if the password is validated.
if st.session_state.get("password_correct", False):
return True
# Show input for password.
st.text_input(
"Password", type="password", on_change=password_entered, key="password"
)
if "password_correct" in st.session_state:
st.error("😕 Password incorrect")
return False
if not check_password():
st.stop() # Do not continue if check_password is not True.
# Main Streamlit app starts here
st.write("Here goes your normal Streamlit app...")
st.button("Click me")
如果一切顺利,你的应用程序应该看起来像这样:

Option 2: Individual password for each user
此选项允许您为应用程序的每个用户设置用户名和密码。与选项1类似,这两个值将使用秘密管理存储在应用程序的秘密中。
Step 1: Add usernames & passwords to your local app secrets
您的本地 Streamlit 应用程序将从应用程序根目录中的文件 .streamlit/secrets.toml 读取密钥。如果该文件尚不存在,请创建此文件,并按照以下所示添加用户名和密码:
# .streamlit/secrets.toml
[passwords]
# Follow the rule: username = "password"
alice_foo = "streamlit123"
bob_bar = "mycrazypw"
重要
请确保将此文件添加到您的.gitignore中,以免提交您的秘密!
或者,您可以通过电子表格或数据库设置和管理用户名和密码。要使用密钥安全地连接到Google Sheets、AWS和其他数据提供商,请阅读我们的教程,了解如何将Streamlit连接到数据源。
Step 2: Copy your app secrets to the cloud
由于上面的secrets.toml文件没有提交到GitHub,你需要将其内容单独传递给你部署的应用程序(在Streamlit社区云上)。转到应用程序仪表板,在应用程序的下拉菜单中,点击编辑Secrets。将secrets.toml的内容复制到文本区域。更多信息可在Secrets管理中找到。

Step 3: Ask for username & password in your Streamlit app
将下面的代码复制到您的Streamlit应用程序中,在底部的check_password()函数调用下方插入您的正常应用程序代码,然后运行它:
# streamlit_app.py
import hmac
import streamlit as st
def check_password():
"""Returns `True` if the user had a correct password."""
def login_form():
"""Form with widgets to collect user information"""
with st.form("Credentials"):
st.text_input("Username", key="username")
st.text_input("Password", type="password", key="password")
st.form_submit_button("Log in", on_click=password_entered)
def password_entered():
"""Checks whether a password entered by the user is correct."""
if st.session_state["username"] in st.secrets[
"passwords"
] and hmac.compare_digest(
st.session_state["password"],
st.secrets.passwords[st.session_state["username"]],
):
st.session_state["password_correct"] = True
del st.session_state["password"] # Don't store the username or password.
del st.session_state["username"]
else:
st.session_state["password_correct"] = False
# Return True if the username + password is validated.
if st.session_state.get("password_correct", False):
return True
# Show inputs for username + password.
login_form()
if "password_correct" in st.session_state:
st.error("😕 User not known or password incorrect")
return False
if not check_password():
st.stop()
# Main Streamlit app starts here
st.write("Here goes your normal Streamlit app...")
st.button("Click me")
如果一切顺利,你的应用程序应该看起来像这样:

还有问题吗?
我们的 论坛 充满了有用的信息和Streamlit专家。