login_attempt.py 1.3 KB

12345678910111213141516171819202122232425262728
  1. """
  2. 登录尝试记录模型
  3. 用于记录登录失败次数,支持账号锁定功能
  4. """
  5. from datetime import datetime
  6. from app.utils.timezone import beijing_now_naive
  7. from sqlalchemy import Column, Integer, String, DateTime, Boolean
  8. from app.database import Base
  9. class LoginAttempt(Base):
  10. """
  11. 登录尝试记录表
  12. 用于记录每次登录尝试(成功/失败),支持登录失败次数限制和账号锁定
  13. """
  14. __tablename__ = "login_attempts"
  15. id = Column(Integer, primary_key=True, autoincrement=True, comment="主键ID")
  16. username = Column(String(100), nullable=False, index=True, comment="用户名")
  17. success = Column(Boolean, default=False, comment="是否成功: 1-成功, 0-失败")
  18. machine_code = Column(String(255), nullable=True, comment="机器码")
  19. ip_address = Column(String(45), nullable=True, comment="IP地址")
  20. user_agent = Column(String(500), nullable=True, comment="用户代理")
  21. failure_reason = Column(String(255), nullable=True, comment="失败原因")
  22. attempted_at = Column(DateTime, default=beijing_now_naive, index=True, comment="尝试时间")
  23. def __repr__(self):
  24. return f"<LoginAttempt(id={self.id}, username='{self.username}', success={self.success}, attempted_at={self.attempted_at})>"