Streamlit-Authenticator使用教程

Streamlit-Authenticator使用教程

Streamlit-AuthenticatorA secure authentication module to validate user credentials in a Streamlit application.项目地址:https://gitcode.com/gh_mirrors/st/Streamlit-Authenticator

1. 项目介绍

Streamlit-Authenticator 是一个专为 Streamlit 应用设计的安全认证模块,它允许开发者轻松集成用户验证功能。此项目由 Mohammad Khorasani 开发并维护,旨在解决在 Streamlit 应用程序中实现用户权限管理和认证的需求。采用MIT许可证开源,支持Python 3.6及更高版本,确保用户凭证安全验证,并且支持自定义配置来增强应用安全性。

2. 项目快速启动

要迅速地在你的Streamlit应用中集成Streamlit-Authenticator,遵循以下步骤:

首先,通过pip安装streamlit-authenticator库:

pip install streamlit-authenticator==0.1.4

请注意,实际开发时可能已有更新版本,建议查看最新版本进行安装。

接下来,在你的Streamlit脚本中引入必要的模块,并配置用户信息。创建或修改一个YAML配置文件(例如 config.yaml)来存储用户名、密码等信息,密码将自动被哈希处理。

示例配置文件 (config.yaml):

names: [User1, User2]
usernames: [user1, user2]
passwords: ['password1_hashed', 'password2_hashed']

密码应该是预先哈希过的值,以保证安全性。你可以使用提供的工具或者直接在第一次设置时使用库函数来哈希密码。

然后,在脚本中导入库并使用:

import streamlit as st
import streamlit_authenticator as stauth

# 从config.yaml加载用户数据
with open('config.yaml') as file:
    config = yaml.safe_load(file)

credentials = {
    'usernames': {
        config['usernames'][0]: {'name': config['names'][0], 'password': config['passwords'][0]},
        config['usernames'][1]: {'name': config['names'][1], 'password': config['passwords'][1]}
    }
}

authenticator = stauth.Authenticate(credentials, 'cookie_name', 'random_key', 30)

name, authentication_status, username = authenticator.login('Login', 'main')

if authentication_status:
    st.write(f'Welcome *{name}*')
elif authentication_status == False:
    st.error('Username/password is incorrect')
elif authentication_status == None:
    st.warning('Please enter your username and password')

3. 应用案例和最佳实践

应用Streamlit-Authenticator的最佳实践包括:

  • 用户权限管理:根据用户角色分配不同级别的访问权限。
  • 密码策略:强制执行复杂的密码规则,增加安全性。
  • 会话管理:利用重认证机制保护长时间未操作的会话。
  • 前端整合:无缝集成到Streamlit界面中,提供良好的用户体验,比如登录失败的反馈和注销功能。

通过添加定制化登录组件,确保应用符合特定的安全和用户体验要求。

4. 典型生态项目

虽然Streamlit-Authenticator本身是Streamlit生态中的一个小部分,但它的存在促进了更多需要用户验证的复杂应用的发展,如:

  • 数据分析平台:为不同的团队成员提供访问受限数据分析视图的能力。
  • 内部工具:确保只有授权员工能够访问敏感的内部管理系统。
  • 教育应用:控制学生对课程资源的访问权限。

通过Streamlit-Authenticator,开发者可以轻易地将其项目拓展至需要身份验证的任何领域,增强了应用的专业性和安全性。


以上即为基于mkhorasani/Streamlit-Authenticator项目的简明教程。记住,有效的文档和实践结合是成功集成此类库的关键。始终保持你的依赖是最新的,并遵循最佳的安全实践。

Streamlit-AuthenticatorA secure authentication module to validate user credentials in a Streamlit application.项目地址:https://gitcode.com/gh_mirrors/st/Streamlit-Authenticator

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

石玥含Lane

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值