credentials:
usernames:
liba001:
email: [email protected]
name: John Smith
password: abc # To be replaced with hashed password
liba002:
email: [email protected]
name: Rebecca Briggs
password: def # To be replaced with hashed password
cookie:
expiry_days: 365
key: random_signature_key # Must be string
name: random_cookie_name
preauthorized:
emails:
- [email protected]
使用 Hasher 模块将纯文本密码转换为哈希密码:
import streamlit_authenticator as stauth
hashed_passwords = stauth.Hasher(['abc', 'def']).generate()
print(hashed_passwords)
将 YAML 文件中的纯文本密码替换为生成的哈希密码
开始主程序
import yaml
from yaml.loader import SafeLoader
with open('config.yaml') as file:
config = yaml.load(file, Loader=SafeLoader)
authenticator = Authenticate(
config['credentials'],
config['cookie']['name'],
config['cookie']['key'],
config['cookie']['expiry_days'],
config['preauthorized']
)
name, authentication_status, username = authenticator.login('Login', 'main')
if authentication_status:
authenticator.logout('Logout', 'main')
st.write(f'Welcome *{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')
pi@raspberrypi:~ $ mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 38
Server version: 10.5.19-MariaDB-0+deb11u2 Debian 11
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> create database mydb;
Query OK, 1 row affected (0.001 sec)
MariaDB [(none)]> use mydb;
Database changed
MariaDB [mydb]> CREATE TABLE salesdata(
-> user_id INT NOT NULL AUTO_INCREMENT,
-> username VARCHAR(40) NOT NULL,
-> password VARCHAR(40) NOT NULL,
-> PRIMARY KEY(user_id)
-> );
MariaDB [mydb]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mydb |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.001 sec)
MariaDB [mydb]>