Class ExponentialLockoutPolicy

java.lang.Object
io.leandev.appfuse.security.lockout.core.ExponentialLockoutPolicy
All Implemented Interfaces:
LockoutPolicy

public class ExponentialLockoutPolicy extends Object implements LockoutPolicy

指數遞增鎖定策略

鎖定時間計算公式:offset + base ^ (失敗次數 - 閾值 + 1) × unit

範例 1(無 offset):

base = 2, threshold = 5, unit = 1 分鐘, offset = 0
- 第 5 次失敗 → 2^1 = 2 分鐘
- 第 6 次失敗 → 2^2 = 4 分鐘
- 第 7 次失敗 → 2^3 = 8 分鐘

範例 2(有 offset):

base = 2, threshold = 5, unit = 1 分鐘, offset = 10 分鐘
- 第 5 次失敗 → 10 + 2 = 12 分鐘
- 第 6 次失敗 → 10 + 4 = 14 分鐘
- 第 7 次失敗 → 10 + 8 = 18 分鐘

適用場景:高安全性需求,快速增加攻擊成本。

注意:為避免過長的鎖定時間,會限制最大指數值。

  • Constructor Details

    • ExponentialLockoutPolicy

      public ExponentialLockoutPolicy(int threshold, int base, int maxExponent, Duration unit)
      建構指數遞增鎖定策略(向後相容)
      Parameters:
      threshold - 觸發鎖定的失敗次數閾值(如 5)
      base - 指數底數(如 2)
      maxExponent - 最大指數值,避免時間過長(如 10 表示最多 2^10 = 1024 倍)
      unit - 時間單位(如 Duration.ofMinutes(1))
    • ExponentialLockoutPolicy

      public ExponentialLockoutPolicy(int threshold, int base, int maxExponent, Duration unit, Duration offset)
      建構指數遞增鎖定策略(含起始偏移)
      Parameters:
      threshold - 觸發鎖定的失敗次數閾值(如 5)
      base - 指數底數(如 2)
      maxExponent - 最大指數值,避免時間過長(如 10 表示最多 2^10 = 1024 倍)
      unit - 時間單位(如 Duration.ofMinutes(1))
      offset - 起始鎖定時間偏移(如 Duration.ofMinutes(10))
    • ExponentialLockoutPolicy

      public ExponentialLockoutPolicy(int threshold, int base, Duration unit)
      建構指數遞增鎖定策略(使用預設最大指數 10)
      Parameters:
      threshold - 觸發鎖定的失敗次數閾值
      base - 指數底數
      unit - 時間單位
  • Method Details

    • getThreshold

      public int getThreshold()
      Description copied from interface: LockoutPolicy
      取得觸發鎖定的失敗次數閾值
      Specified by:
      getThreshold in interface LockoutPolicy
      Returns:
      閾值(如 5 表示連續失敗 5 次時觸發鎖定)
    • calculateLockoutDuration

      public Duration calculateLockoutDuration(int failureCount)
      Description copied from interface: LockoutPolicy
      根據失敗次數計算鎖定時間
      Specified by:
      calculateLockoutDuration in interface LockoutPolicy
      Parameters:
      failureCount - 失敗次數
      Returns:
      鎖定時間