在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.2 May 2023 Added a unique key for the logout button to prevent
duplicate key errors.
v0.2.1 Jun 25, 2022
v0.1.5 May 24, 2022 Enhanced reauthentication cookie checking,Featured
ability to import credentials and cookie setting

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

最后

从时代发展的角度看,网络安全的知识是学不完的,而且以后要学的会更多,同学们要摆正心态,既然选择入门网络安全,就不能仅仅只是入门程度而已,能力越强机会才越多。

因为入门学习阶段知识点比较杂,所以我讲得比较笼统,大家如果有不懂的地方可以找我咨询,我保证知无不言言无不尽,需要相关资料也可以找我要,我的网盘里一大堆资料都在吃灰呢。

干货主要有:

①1000+CTF历届题库(主流和经典的应该都有了)

②CTF技术文档(最全中文版)

③项目源码(四五十个有趣且经典的练手项目及源码)

④ CTF大赛、web安全、渗透测试方面的视频(适合小白学习)

⑤ 网络安全学习路线图(告别不入流的学习)

⑥ CTF/渗透测试工具镜像文件大全

⑦ 2023密码学/隐身术/PWN技术手册大全

如果你对网络安全入门感兴趣,那么你需要的话可以点击这里👉网络安全重磅福利:入门&进阶全套282G学习资源包免费分享!

扫码领取

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐