Files
asd-backend/migrations/005_add_web_login_codes.sql
Milky0217 e6048ea010 feat(auth): 实现登录限流和性能优化
性能优化:
- 添加数据库索引优化查询性能 (006)
  - weather_data: user_id, date, is_favorite 索引
  - users: openid 索引
  - payment_orders: status 索引
- 新增 refresh_tokens 表支持双 Token 机制 (004)
- 新增 web_login_codes 表支持网页端扫码登录 (005)

安全增强:
- 实现基于 IP 的登录限流 (rate_limiter.rs)
  - 滑动窗口算法: 5次/分钟/IP
  - 自动清理过期记录
  - 429 TooManyRequests 响应

新模块:
- src/rate_limiter.rs: 限流模块
- src/alipay.rs: 支付宝签名模块 (RSA2)
- src/error.rs: 统一错误类型 (含 TooManyRequests)
- src/handlers/meta.rs: 元数据处理器

代码清理:
- 修复 .gitignore 规则,正确跟踪 src/ 和 migrations/
2026-04-24 16:13:49 +08:00

28 lines
1.3 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- ============================================
-- 迁移: 005_add_web_login_codes.sql
-- 目的: 添加网页端微信扫码登录临时码表
-- 日期: 2026-04-20
-- 依赖: users 表001 创建)
-- 说明: 用户扫码后生成临时登录码,前端轮询验证登录状态
-- ============================================
-- 05_add_web_login_codes.sql
-- 网页端微信扫码登录:临时登录码表
CREATE TABLE IF NOT EXISTS web_login_codes (
id SERIAL PRIMARY KEY,
code VARCHAR(32) UNIQUE NOT NULL, -- 登录码,如 ASD-ABC123
user_id INTEGER, -- 关联用户(确认登录后写入)
openid VARCHAR(128), -- 用户 openid
token TEXT, -- 生成的 JWT确认后写入
expires_at TIMESTAMPTZ NOT NULL, -- 过期时间10分钟内有效
created_at TIMESTAMPTZ DEFAULT NOW()
);
-- 登录码索引
CREATE INDEX IF NOT EXISTS idx_web_login_codes_code ON web_login_codes(code);
CREATE INDEX IF NOT EXISTS idx_web_login_codes_expires ON web_login_codes(expires_at);
-- 定期清理过期登录码(可由 cron 或服务启动时触发)
-- DELETE FROM web_login_codes WHERE expires_at < NOW();