Streamlit应用程序使用Streamlit-Authenticator进行用户的安全身份验证实践

1. 什么是Streamlit?

让python代码快速生成web app是很多AI算法工程师们的需求,2019年新兴的这个streamlit项目能帮你解决类似的问题。Python应用程序框架Streamlit,是一个开源的Python库,在github(https://github.com/streamlit/streamlit)上超过18.9千颗stars、1.7千folk(截止2022.5.6),利用Streamlit可以快速构建机器学习应用和高级数据分析可视化的用户界面。

2. Streamlit-Authenticator安全身份验证组件

在Streamlit官方文档中,没有提供提供安全身份验证组件。目前,第三方streamlit-authenticator(https://github.com/mkhorasani/Streamlit-Authenticator)提供此组件。

2.1. Hashing 密码加密

hashed_passwords = stauth.Hasher(passwords).generate()

2.2. 创建登录窗口组件

使用哈希密码创建身份验证对象。在这里,您需要输入JWT cookie的名称,该名称将存储在客户端浏览器上,用于重新验证用户,而无需重新输入其凭据。此外,您还需要提供任意随机密钥,用于对cookie的签名进行哈希。最后,您需要指定使用cookie的天数,如果不需要无密码重新验证,可以将其设置为0。

authenticator = stauth.Authenticate(names, usernames, hashed_passwords,
    'some_cookie_name', 'some_signature_key', cookie_expiry_days=30)

然后最终呈现登录窗口模块,如下所示。在这里,您需要提供登录表单的名称,并指定表单的位置,即主体或侧栏(默认为主体)。

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

2.3. 认证用户

使用返回的名称和身份验证状态来允许已验证的用户继续访问任何受限制的内容。此外,还可以在主体或侧边栏的任何位置添加可选的注销按钮(默认为主体)。

if authentication_status:
    authenticator.logout('Logout', 'main')
    st.write('Welcome *%s*' % (name))
    st.title('Some content')
elif authentication_status == False:
    st.error('Username/password is incorrect')
elif authentication_status == None:
    st.warning('Please enter your username and password')

其中,登出为authenticator.logout(‘Logout’, ‘main’)函数方法。

3. 登录认证实践

3.1. 界面效果示例

实践效果如下图所示:
在这里插入图片描述
登陆后,进入自己的应用端。
在这里插入图片描述

3.2. 实践代码

使用Streamlit Authenticator非常简单,只需导入模块并调用它来验证预定义用户的凭据。

pip install streamlit-authenticator

关键依赖:
Successfully installed PyJWT-2.3.0 bcrypt-3.2.2 extra-streamlit-components-0.1.53

依赖extra-streamlit-components-0.1.53 提供Cookies管理。详见https://github.com/Mohamed-512/Extra-Streamlit-Components。

主要代码如下:

import streamlit as st
import streamlit_authenticator as stauth
import BigBooks

# 如下代码数据,可以来自数据库
names = ['肖永威', '管理员']
usernames = ['xiaoyw', 'admin']
passwords = ['S0451', 'ad4516']

hashed_passwords = stauth.Hasher(passwords).generate()

authenticator = stauth.Authenticate(names, usernames, hashed_passwords,
    'some_cookie_name', 'some_signature_key', cookie_expiry_days=30)

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

if authentication_status:
    with st.container():
        cols1,cols2 = st.columns(2)
        cols1.write('欢迎 *%s*' % (name))
        with cols2.container():
            authenticator.logout('Logout', 'main')

    BigBooks.main()
elif authentication_status == False:
    st.error('Username/password is incorrect')
elif authentication_status == None:
    st.warning('Please enter your username and password')

代码解释:BigBooks.main()代码引自[1]内容。

首次使用,欢迎爱好者反馈研究。

参考:

[1]. 肖永威. Python+Streamlit+MongoDB GridFS构建低代码文档管理应用(Demo篇). CSDN博客. 2022.04
[2]. 肖永威. Python数据分析师使用低代码Streamlit实现Web数据可视化方法——入门篇. CSDN博客。 2022.01

评论 18
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

肖永威

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

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

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

打赏作者

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

抵扣说明:

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

余额充值