Class InMemoryAttemptStore

java.lang.Object
io.leandev.appfuse.security.lockout.store.InMemoryAttemptStore
All Implemented Interfaces:
AttemptStore

public class InMemoryAttemptStore extends Object implements AttemptStore

記憶體儲存實作

使用 ConcurrentHashMap 儲存失敗次數與鎖定狀態。

適用場景

  • 單機部署
  • 開發與測試環境
  • 小型應用程式

限制

  • 不支援分散式部署(多實例間資料不共享)
  • 應用重啟後資料遺失
  • 記憶體佔用會隨使用者數量增長

建議:生產環境或分散式部署應使用 Redis 或資料庫實作。

執行緒安全性:使用 ConcurrentHashMap 確保執行緒安全。

  • Constructor Details

    • InMemoryAttemptStore

      public InMemoryAttemptStore()
      建構記憶體儲存實作
  • Method Details

    • getFailureCount

      public int getFailureCount(String principal)
      Description copied from interface: AttemptStore
      取得使用者的失敗次數
      Specified by:
      getFailureCount in interface AttemptStore
      Parameters:
      principal - 使用者帳號
      Returns:
      失敗次數,若無記錄則回傳 0
    • incrementFailureCount

      public int incrementFailureCount(String principal)
      Description copied from interface: AttemptStore
      遞增使用者的失敗次數
      Specified by:
      incrementFailureCount in interface AttemptStore
      Parameters:
      principal - 使用者帳號
      Returns:
      遞增後的失敗次數
    • setLockoutTime

      public void setLockoutTime(String principal, LocalDateTime unlockTime)
      Description copied from interface: AttemptStore
      設定使用者的鎖定到期時間
      Specified by:
      setLockoutTime in interface AttemptStore
      Parameters:
      principal - 使用者帳號
      unlockTime - 鎖定到期時間
    • getLockoutTime

      public Optional<LocalDateTime> getLockoutTime(String principal)
      Description copied from interface: AttemptStore
      取得使用者的鎖定到期時間
      Specified by:
      getLockoutTime in interface AttemptStore
      Parameters:
      principal - 使用者帳號
      Returns:
      鎖定到期時間,若無記錄則回傳 empty
    • clear

      public void clear(String principal)
      Description copied from interface: AttemptStore
      清除使用者的所有記錄(失敗次數與鎖定狀態)
      Specified by:
      clear in interface AttemptStore
      Parameters:
      principal - 使用者帳號