timmy / 🔐 Python 密碼雜湊教學:用 bcrypt 安全加密與驗證
0 beğeniler
0 çatallar
1 dosyalar
Son aktivite 8 months ago
用 bcrypt 進行密碼加密與驗證,支援 salt、自動安全雜湊,保護你的使用者資料不被爆破!適合登入驗證與帳號系統 🚀
| 1 | import bcrypt |
| 2 | |
| 3 | |
| 4 | def encode_password(password: str) -> str: |
| 5 | salt = bcrypt.gensalt() |
| 6 | hashed = bcrypt.hashpw(password.encode(), salt) |
| 7 | return hashed.decode() |
| 8 | |
| 9 | |
| 10 | def check_password(password: str, hashed: str) -> bool: |
timmy / 使用 FastAPI 進行 API 身份驗證與請求處理
0 beğeniler
0 çatallar
3 dosyalar
Son aktivite 9 months ago
| 1 | from fastapi import FastAPI, Header, HTTPException |
| 2 | |
| 3 | app = FastAPI() |
| 4 | |
| 5 | @app.get("/secure-data") |
| 6 | async def secure_data(api_key: str = Header(None)): |
| 7 | if api_key != "my_secure_token": |
| 8 | raise HTTPException(status_code=401, detail="無效的 API 金鑰") |
| 9 | return {"message": "驗證成功,提供安全數據"} |
timmy / Flask Google OAuth2 登入
0 beğeniler
0 çatallar
2 dosyalar
Son aktivite 9 months ago
使用 Flask 和 Authlib 整合 Google OAuth2,透過 OpenID Connect 進行身份驗證,讓使用者能安全登入應用程式。實作包括 authorize_redirect 及 parse_id_token,確保驗證的完整性與安全性。
| 1 | GOOGLE_CLIENT_ID=你的 Google Client ID |
| 2 | GOOGLE_CLIENT_SECRET=你的 Google Client Secret |
timmy / JWT 令牌生成與驗證
0 beğeniler
0 çatallar
1 dosyalar
Son aktivite 10 months ago
這段 Python 程式碼使用 JWT(JSON Web Token)來實現 身份驗證與授權。它首先根據 使用者資訊(ID、帳號)及 自訂金鑰 生成一個 有效期限為 30 分鐘 的 JWT,然後透過相同的金鑰來驗證 Token 的有效性,並解碼出原始資料。此機制適用於 使用者登入授權、API 驗證及安全通信,可確保請求的合法性並防止未授權的存取。
| 1 | import jwt |
| 2 | import datetime |
| 3 | |
| 4 | # 假設的使用者資訊 |
| 5 | user = { |
| 6 | 'id': 123, |
| 7 | 'username': 'exampleUser' |
| 8 | } |
| 9 | |
| 10 | # 生成 JWT |
timmy / Streamlit 使用者驗證系統
0 beğeniler
0 çatallar
1 dosyalar
Son aktivite 10 months ago
| 1 | import json |
| 2 | from abc import ABC, abstractmethod |
| 3 | |
| 4 | import streamlit as st |
| 5 | import streamlit_authenticator as stauth |
| 6 | import yaml |
| 7 | |
| 8 | |
| 9 | # 策略介面 |
| 10 | class DataHandlerStrategy(ABC): |
timmy / 使用 Streamlit 建立使用者身份驗證系統
0 beğeniler
0 çatallar
2 dosyalar
Son aktivite 10 months ago
這段程式碼使用 Streamlit 和 streamlit-authenticator 來 管理使用者登入,透過 YAML 設定檔 儲存使用者憑證與 Cookie 配置,並提供 登入、登出 和 使用者驗證 功能。
| 1 | cookie: |
| 2 | expiry_days: 30 |
| 3 | key: some_signature_key |
| 4 | name: my_auth_cookie |
| 5 | credentials: |
| 6 | usernames: |
| 7 | jsmith: |
| 8 | email: jsmith@gmail.com |
| 9 | failed_login_attempts: 0 # Will be managed automatically |
| 10 | logged_in: False # Will be managed automatically |
timmy / 使用 Streamlit 建立簡單使用者驗證系統
0 beğeniler
0 çatallar
1 dosyalar
Son aktivite 10 months ago
這段程式碼使用 streamlit_authenticator 建立一個簡單的登入系統,支持使用者驗證(帳號、密碼)和登出功能,並在側邊欄提供登出按鈕。密碼經過雜湊處理以增加安全性。
| 1 | import streamlit as st # 匯入 streamlit 模組 |
| 2 | import streamlit_authenticator as stauth # 匯入 streamlit_authenticator 模組 |
| 3 | |
| 4 | names = ["John", "Mary"] # 定義名字清單 |
| 5 | usernames = ["john", "mary"] # 定義使用者名清單 |
| 6 | passwords = ["1234", "5678"] # 定義密碼清單 |
| 7 | |
| 8 | hashed_passwords = stauth.Hasher(passwords).generate() # 使用 Hasher 類別對密碼進行雜湊處理 |
| 9 | |
| 10 | authenticator = stauth.Authenticate( # 建立一個 stauth.Authenticate 的實例 authenticator |
Daha yeni
Daha eski