[MD]
# Authme网页注册后端程序
这是一个基于 Flask 的用户注册和密码找回系统,用于与 Authme 数据库集成。
## 功能特性
1. 用户注册 API(包含邮箱验证)
2. 忘记密码 API(包含邮箱验证)
3. MySQL 数据库集成
4. 邮件发送功能
5. 模块化项目结构
6. SHA256密码加密存储
7. 频率限制(每邮箱每分钟只能请求一次验证码)
## API 接口说明
### 注册相关接口
#### POST /api/register/
用户注册,发送验证码到邮箱
**请求参数:**
```json
{
"username": "用户名",
"email": "邮箱地址",
"password": "密码"
}
```
**响应示例:**
```json
{
"success": true,
"message": "验证码已发送至您的邮箱,请查收"
}
```
**频率限制响应示例:**
```json
{
"success": false,
"message": "请求过于频繁,请稍后再试"
}
```
#### POST /api/register/verify
验证注册验证码
**请求参数:**
```json
{
"username": "用户名",
"email": "邮箱地址",
"verification_code": "验证码"
}
```
**响应示例:**
```json
{
"success": true,
"message": "注册成功"
}
```
### 忘记密码相关接口
#### POST /api/forgot-password/
发送密码重置验证码到邮箱
**请求参数:**
```json
{
"username": "用户名",
"email": "邮箱地址"
}
```
**响应示例:**
```json
{
"success": true,
"message": "验证码已发送至您的邮箱,请查收"
}
```
**频率限制响应示例:**
```json
{
"success": false,
"message": "请求过于频繁,请稍后再试"
}
```
#### POST /api/forgot-password/reset
重置密码
**请求参数:**
```json
{
"username": "用户名",
"email": "邮箱地址",
"verification_code": "验证码",
"new_password": "新密码"
}
```
**响应示例:**
```json
{
"success": true,
"message": "密码重置成功"
}
```
## 安全性说明
- 所有密码均使用 SHA256 算法进行加密存储
- 验证码发送机制确保操作安全性
- 防止用户枚举攻击(无论用户是否存在都返回相同响应)
- 频率限制防止恶意刷验证码
## 注意事项
1. 在生产环境中,请务必修改 `SECRET_KEY`
2. 验证码应设置有效期
3. 当前频率限制基于内存存储,在多实例部署时需要使用Redis等共享存储方案
4. 可以考虑使用更强的密码哈希算法如 bcrypt 或 scrypt(SHA256仅作演示用途)
5. 打包后的exe文件较大,因为它包含了Python解释器和所有依赖项
[/MD]
请手动创建config.yml文件,示例如下:
示例注册页面:
下载链接:https://www.alipan.com/s/Q9kAkfsVPhj
请手动创建config.yml文件,示例如下:
XML:
SECRET_KEY: your-secret-key-change-in-production
MYSQL_HOST: localhost
MYSQL_PORT: 3306
MYSQL_USER: root
MYSQL_PASSWORD: ""
MYSQL_DB: authme
MYSQL_TABLE: authme
MAIL_SERVER: smtp.gmail.com
MAIL_PORT: 587
MAIL_USE_TLS: true
MAIL_USERNAME: your-email@gmail.com
MAIL_PASSWORD: your-app-password
MAIL_DEFAULT_SENDER: your-email@gmail.com
HTML:
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<title>用户注册</title>
<style>
body { font-family: sans-serif; background: #f5f7fa; margin: 0; padding: 20px; }
.container { max-width: 400px; margin: 40px auto; background: #fff; padding: 30px; border-radius: 8px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); }
h2 { text-align: center; margin-bottom: 20px; }
.form-group { margin-bottom: 16px; }
label { display: block; margin-bottom: 4px; font-weight: bold; }
input { width: 100%; padding: 8px; border: 1px solid #ccc; border-radius: 4px; box-sizing: border-box; }
.captcha-box { display: flex; gap: 8px; align-items: center; }
#imgCaptcha { cursor: pointer; user-select: none; font-family: monospace; font-size: 20px; padding: 6px 10px; background: #eee; border-radius: 4px; }
button { width: 100%; padding: 10px; background: #007bff; color: white; border: none; border-radius: 4px; cursor: pointer; }
button:disabled { background: #ccc; }
.msg { margin-top: 10px; text-align: center; color: #e74c3c; }
.success { color: #27ae60; }
</style>
</head>
<body>
<div class="container">
<h2>用户注册</h2>
<form id="registerForm">
<div class="form-group">
<label for="username">用户名</label>
<input type="text" id="username" required />
</div>
<div class="form-group">
<label for="email">邮箱</label>
<input type="email" id="email" required />
</div>
<div class="form-group">
<label for="password">密码</label>
<input type="password" id="password" required minlength="6" />
</div>
<div class="form-group">
<label for="confirmPassword">确认密码</label>
<input type="password" id="confirmPassword" required />
</div>
<div class="form-group">
<label>图形验证码</label>
<div class="captcha-box">
<span id="imgCaptcha" onclick="refreshCaptcha()">0000</span>
<input type="text" id="captcha" placeholder="输入上方验证码" required maxlength="4" style="flex:1" />
</div>
</div>
<div class="form-group">
<label>邮箱验证码</label>
<div style="display: flex; gap: 8px; align-items: center;">
<input type="text" id="emailCode" placeholder="6位验证码" required maxlength="6"
style="width: 35em; padding: 8px; text-align: center;" />
<button type="button" id="btnSendEmailCode"
style="padding: 6px 12px; font-size: 14px; white-space: nowrap;"
onclick="sendEmailCode()">获取验证码</button>
</div>
</div>
<button type="submit">注册</button>
<div id="msg"></div>
</form>
</div>
<script>
let captchaCode = '';
function generateCaptcha() {
captchaCode = Math.floor(1000 + Math.random() * 9000).toString(); // 4位数字
document.getElementById('imgCaptcha').innerText = captchaCode;
}
function refreshCaptcha() { generateCaptcha(); }
generateCaptcha();
async function sendEmailCode() {
const username = document.getElementById('username').value.trim();
const email = document.getElementById('email').value.trim();
const captcha = document.getElementById('captcha').value.trim();
if (!username || !email) return showError('请先填写用户名和邮箱');
if (captcha !== captchaCode) return showError('图形验证码错误');
const btn = document.getElementById('btnSendEmailCode');
btn.disabled = true;
btn.innerText = '发送中...';
try {
const res = await fetch('/api/register/', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ username, email, password: 'dummy' }) // 后端只用 username+email 发验证码
});
const data = await res.json();
if (data.success) {
showSuccess('邮箱验证码已发送,请查收');
// 倒计时60秒防重复点击
let count = 60;
const timer = setInterval(() => {
if (count <= 0) {
clearInterval(timer);
btn.disabled = false;
btn.innerText = '重发';
} else {
btn.innerText = `${count}s`;
count--;
}
}, 1000);
} else {
showError(data.message || '发送失败');
btn.disabled = false;
btn.innerText = '获取';
}
} catch (e) {
showError('网络错误');
btn.disabled = false;
btn.innerText = '获取';
}
}
async function registerFormSubmit(e) {
e.preventDefault();
const username = document.getElementById('username').value.trim();
const email = document.getElementById('email').value.trim();
const password = document.getElementById('password').value;
const confirmPassword = document.getElementById('confirmPassword').value;
const captcha = document.getElementById('captcha').value.trim();
const emailCode = document.getElementById('emailCode').value.trim();
if (!username || !email || !password || !confirmPassword || !captcha || !emailCode) {
return showError('请填写所有字段');
}
if (password !== confirmPassword) return showError('两次密码不一致');
if (captcha !== captchaCode) return showError('图形验证码错误');
try {
const res = await fetch('/api/register/verify', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ username, email, verification_code: emailCode })
});
const data = await res.json();
if (data.success) {
showSuccess('注册成功!');
// 可跳转或清空表单
} else {
showError(data.message || '注册失败');
}
} catch (e) {
showError('网络错误');
}
}
function showError(msg) {
const el = document.getElementById('msg');
el.className = 'msg';
el.innerText = msg;
}
function showSuccess(msg) {
const el = document.getElementById('msg');
el.className = 'msg success';
el.innerText = msg;
}
document.getElementById('registerForm').addEventListener('submit', registerFormSubmit);
</script>
</body>
</html>
- 内容版权许可
- 作者保留一切权利,禁止转载