Class DefaultLoginAttemptTracker

java.lang.Object
io.leandev.appfuse.security.lockout.core.DefaultLoginAttemptTracker
All Implemented Interfaces:
LoginAttemptTracker

public class DefaultLoginAttemptTracker extends Object implements LoginAttemptTracker

預設登入嘗試追蹤器實作

整合 AttemptStoreLockoutPolicy,提供完整的登入鎖定機制。

業務邏輯

  • 記錄失敗:累加失敗次數,達到閾值時觸發鎖定
  • 檢查鎖定:判斷鎖定是否仍有效(到期時間 > 當前時間)
  • 清除記錄:登入成功時清除所有記錄

鎖定期間行為

  • 鎖定期間的登入嘗試不增加失敗次數
  • 鎖定到期後自動解除,使用者可繼續嘗試
  • 鎖定到期後若再次失敗,失敗次數繼續累計

執行緒安全性:依賴 AttemptStore 實作的執行緒安全性。

  • Constructor Details

    • DefaultLoginAttemptTracker

      public DefaultLoginAttemptTracker(AttemptStore store, LockoutPolicy policy)
      建構預設登入嘗試追蹤器
      Parameters:
      store - 儲存機制
      policy - 鎖定策略
  • Method Details

    • recordFailure

      public int recordFailure(String principal)
      Description copied from interface: LoginAttemptTracker

      記錄登入失敗

      每次呼叫會累加失敗次數,當達到閾值時自動觸發鎖定機制。

      Specified by:
      recordFailure in interface LoginAttemptTracker
      Parameters:
      principal - 使用者帳號
      Returns:
      當前失敗次數
    • isLocked

      public boolean isLocked(String principal)
      Description copied from interface: LoginAttemptTracker

      檢查帳號是否已鎖定

      判斷標準:鎖定到期時間存在且晚於當前時間。

      Specified by:
      isLocked in interface LoginAttemptTracker
      Parameters:
      principal - 使用者帳號
      Returns:
      true 表示已鎖定,false 表示未鎖定
    • getRemainingLockoutTime

      public Optional<Duration> getRemainingLockoutTime(String principal)
      Description copied from interface: LoginAttemptTracker
      取得剩餘鎖定時間
      Specified by:
      getRemainingLockoutTime in interface LoginAttemptTracker
      Parameters:
      principal - 使用者帳號
      Returns:
      剩餘時間,若未鎖定或已過期則回傳 empty
    • clearAttempts

      public void clearAttempts(String principal)
      Description copied from interface: LoginAttemptTracker

      清除失敗記錄

      通常在登入成功時呼叫,清除該使用者的所有失敗記錄與鎖定狀態。

      Specified by:
      clearAttempts in interface LoginAttemptTracker
      Parameters:
      principal - 使用者帳號
    • getFailureCount

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