Package net.targetr.wtm3.util
Class AttackDetection
java.lang.Object
net.targetr.wtm3.util.AttackDetection
Provides mechanisms to detect and mitigate potential denial-of-service (DoS) attacks
or abuse by monitoring request rates from specific client identifiers (like IP addresses).
This class implements a flexible rate-limiting strategy, tracking the number of requests
made by each client identifier within a customizable rolling time window. If an identifier exceeds
the predefined maximum number of requests within the specified time unit, a SecurityException is thrown.
-
Constructor Summary
ConstructorsConstructorDescriptionAttackDetection(int maxRequestsCount, long timeUnit) Constructs a new AttackDetection instance with customizable rate limits. -
Method Summary
Modifier and TypeMethodDescriptionvoidChecks if the given id has exceeded the allowed request rate limit.voidRemoves all request rate records that are older than the configured time unit.
-
Constructor Details
-
AttackDetection
public AttackDetection(int maxRequestsCount, long timeUnit) Constructs a new AttackDetection instance with customizable rate limits.- Parameters:
maxRequestsCount- the maximum allowed requests within the configured time windowtimeUnit- the duration of the rate-limiting time window in milliseconds
-
-
Method Details
-
checkRequestRate
Checks if the given id has exceeded the allowed request rate limit.If the limit is exceeded, a
SecurityExceptionis thrown. This method also triggers periodic cleanup of expired rate records.- Parameters:
id- the id of the client making the request- Throws:
SecurityException- if the id has exceeded the maximum allowed requests in the configured time unit
-
clearExpiredRates
public void clearExpiredRates()Removes all request rate records that are older than the configured time unit.This method iterates through the currently tracked records and discards those whose initial request timestamp is older than the configured time unit threshold.
-