Streamlit应用程序使用Streamlit-Authenticator进行用户的安全身份验证实践(解决升级问题)

在Streamlit官方文档中,没有提供提供安全身份验证组件。目前,第三方streamlit-authenticator提供此功能,详见引用我原来的博文,在《Streamlit应用程序使用Streamlit-Authenticator进行用户的安全身份验证实践》文中,原使用的代码报错:

authenticator = stauth.Authenticate(names, usernames, hashed_passwords,
TypeError: __init__() got multiple values for argument 'cookie_expiry_days'

报错原因是Streamlit-Authenticator包升级了,原代码发生了改变,解决办法如下:

import streamlit as st
import streamlit_authenticator as stauth
import PVForecastWeb

# 此行代码被强制要求放在第一行。
st.set_page_config(layout="wide") #设置屏幕展开方式,宽屏模式布局更好

credentials = {'usernames': {
                'xiaoyw': {'email': 'xiaoyw****@gmail.com',
                            'name': '肖永威',
                            'password': '*************'},   
                'admin': {'email': 'admin***@gmail.com',
                            'name': '管理员',
                            'password': '************  '} 
                            }
               }

authenticator = stauth.Authenticate(credentials,
    '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')

    PVForecastWeb.main()  # 进入业务应用
elif authentication_status == False:
    st.error('Username/password is incorrect')
elif authentication_status == None:
    st.warning('Please enter your username and password')

其中,密码使用其提供的函数加密,输出字符串沾到代码中即可。

hashed_passwords = stauth.Hasher(['S0451', 'ad4516']).generate()

在这里插入图片描述

当然,可以使用其帮助https://github.com/mkhorasani/Streamlit-Authenticator/tree/main中的yaml配置文件,在此不再累述。

版本变化情况:

版本号时间说明
v0.2.2May 2023Added a unique key for the logout button to prevent duplicate key errors.
v0.2.1Jun 25, 2022
v0.1.5May 24, 2022Enhanced reauthentication cookie checking,Featured ability to import credentials and cookie setting

参考:
肖永威. Streamlit应用程序使用Streamlit-Authenticator进行用户的安全身份验证实践. CSDN博客. 2022.05

评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

肖永威

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

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

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

打赏作者

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

抵扣说明:

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

余额充值