RateLimitFilter (Apache Tomcat 11.0.24 API Documentation)
Description: declaration: package: org.apache.catalina.filters, class: RateLimitFilter

# Class RateLimitFilter

java.lang.Object
org.apache.catalina.filters.FilterBase
org.apache.catalina.filters.RateLimitFilter

All Implemented Interfaces:
  `Filter`

public class RateLimitFilter extends FilterBase

Servlet filter that can help mitigate Denial of Service (DoS) and Brute Force attacks by limiting the number of a requests that are allowed from a single IP address within a time window (also referred to as a time bucket), e.g. 300 Requests per 60 seconds.

The filter works by incrementing a counter in a time bucket for each IP address, and if the counter exceeds the allowed limit then further requests from that IP are dropped with a "429 Too many requests" response until the bucket time ends and a new bucket starts.

The RateLimiter implementation can be set via the `rateLimitClassName` init param. The default implementation, `org.apache.catalina.util.FastRateLimiter`, is optimized for efficiency and low overhead so it converts some configured values to more efficient values. For example, a configuration of a 60 seconds time bucket is converted to 65.536 seconds. That allows for very fast bucket calculation using bit shift arithmetic. In order to remain true to the user intent, the configured number of requests is then multiplied by the same ratio, so a configuration of 100 Requests per 60 seconds, has the real values of 109 Requests per 65 seconds. An alternative implementation, `org.apache.catalina.util.ExactRateLimiter`, is intended to provide a less efficient but more accurate control, whose effective duration in seconds and number of requests configuration are consist with the user declared. You can specify a different class as long as it implements the `org.apache.catalina.util.RateLimiter` interface.

It is common to set up different restrictions for different URIs. For example, a login page or authentication script is typically expected to get far fewer requests than the rest of the application, so you can add a filter definition that would allow only 5 requests per 15 seconds and map those URIs to it.

You can set `enforce` to `false` to disable the termination of requests that exceed the allowed limit. Then your application code can inspect the Request Attribute `org.apache.catalina.filters.RateLimitFilter.Count` and decide how to handle the request based on other information that it has, e.g. allow more requests to certain users based on roles, etc.

`exposeHeaders` enables the output of the rate limiter configuration and state via a response header as per RateLimit header fields for HTTP (draft).

WARNING: if Tomcat is behind a reverse proxy then you must make sure that the Rate Limit Filter sees the client IP address, so if for example you are using the Remote IP Filter, then the filter mapping for the Rate Limit Filter must come after the mapping of the Remote IP Filter to ensure that each request has its IP address resolved before the Rate Limit Filter is applied. Failure to do so will count requests from different IPs in the same bucket and will result in a self-inflicted DoS attack.

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `static final int`
  `DEFAULT_BUCKET_DURATION`
  Default duration in seconds.
  `static final int`
  `DEFAULT_BUCKET_REQUESTS`
  Default number of requests per duration.
  `static final boolean`
  `DEFAULT_ENFORCE`
  Default value for enforce.
  `static final boolean`
  `DEFAULT_EXPOSE_HEADERS`
  Default value of the expose headers flag.
  `static final int`
  `DEFAULT_STATUS_CODE`
  Default status code to return if requests per duration is exceeded.
  `static final String`
  `DEFAULT_STATUS_MESSAGE`
  Default status message to return if requests per duration is exceeded.
  `static final String`
  `HEADER_RATE_LIMIT`
  Name of the rate limit remaining quota header field defined in RateLimit header fields for HTTP (draft).
  `static final String`
  `HEADER_RATE_LIMIT_POLICY`
  Name of the rate limit policy header field defined in RateLimit header fields for HTTP (draft).
  `static final String`
  `RATE_LIMIT_ATTRIBUTE_COUNT`
  Request attribute that will contain the number of requests per duration.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `RateLimitFilter()`
  Default constructor.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `destroy()`
  Called by the web container to indicate to a filter that it is being taken out of service.
  `void`
  `doFilter(ServletRequest request, ServletResponse response, FilterChain chain)`
  The `doFilter` method of the Filter is called by the container each time a request/response pair is passed through the chain due to a client request for a resource at the end of the chain.
  `protected Log`
  `getLogger()`
  Returns the logger for this filter.
  `void`
  `init(FilterConfig filterConfig)`
  Iterates over the configuration parameters and either logs a warning, or throws an exception for any parameter that does not have a matching setter in this filter.
  `protected boolean`
  `isConfigProblemFatal()`
  Determines if an exception when calling a setter or an unknown configuration attribute triggers the failure of this filter which in turn will prevent the web application from starting.
  `void`
  `setBucketDuration(int bucketDuration)`
  Set the duration of the time bucket in seconds.
  `void`
  `setBucketRequests(int bucketRequests)`
  Set the maximum number of requests allowed per time bucket.
  `void`
  `setEnforce(boolean enforce)`
  Set whether to enforce the rate limit by rejecting requests that exceed the limit.
  `void`
  `setExposeHeaders(boolean exposeHeaders)`
  Set whether to expose rate limit headers in the HTTP response.
  `void`
  `setPolicyName(String policyName)`
  Set the name of the rate limit policy for use in response headers.
  `void`
  `setRateLimitClassName(String rateLimitClassName)`
  Set the fully qualified class name of the `RateLimiter` implementation to use.
  `void`
  `setStatusCode(int statusCode)`
  Set the HTTP status code to return when the rate limit is exceeded.
  `void`
  `setStatusMessage(String statusMessage)`
  Set the status message to return when the rate limit is exceeded.

  ### Methods inherited from class Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Field Details

  - ### DEFAULT_BUCKET_DURATION

    public static final int DEFAULT_BUCKET_DURATION
    Default duration in seconds.
    See Also:
      - Constant Field Values

  - ### DEFAULT_BUCKET_REQUESTS

    public static final int DEFAULT_BUCKET_REQUESTS
    Default number of requests per duration.
    See Also:
      - Constant Field Values

  - ### DEFAULT_ENFORCE

    public static final boolean DEFAULT_ENFORCE
    Default value for enforce.
    See Also:
      - Constant Field Values

  - ### DEFAULT_EXPOSE_HEADERS

    public static final boolean DEFAULT_EXPOSE_HEADERS
    Default value of the expose headers flag.
    See Also:
      - Constant Field Values

  - ### HEADER_RATE_LIMIT_POLICY

    public static final String HEADER_RATE_LIMIT_POLICY
    Name of the rate limit policy header field defined in RateLimit header fields for HTTP (draft).
    See Also:
      - Constant Field Values

  - ### HEADER_RATE_LIMIT

    public static final String HEADER_RATE_LIMIT
    Name of the rate limit remaining quota header field defined in RateLimit header fields for HTTP (draft).
    See Also:
      - Constant Field Values

  - ### DEFAULT_STATUS_CODE

    public static final int DEFAULT_STATUS_CODE
    Default status code to return if requests per duration is exceeded.
    See Also:
      - Constant Field Values

  - ### DEFAULT_STATUS_MESSAGE

    public static final String DEFAULT_STATUS_MESSAGE
    Default status message to return if requests per duration is exceeded.
    See Also:
      - Constant Field Values

  - ### RATE_LIMIT_ATTRIBUTE_COUNT

    public static final String RATE_LIMIT_ATTRIBUTE_COUNT
    Request attribute that will contain the number of requests per duration.
    See Also:
      - Constant Field Values

- ## Constructor Details

  - ### RateLimitFilter

    public RateLimitFilter()
    Default constructor.

- ## Method Details

  - ### setBucketDuration

    public void setBucketDuration(int bucketDuration)
    Set the duration of the time bucket in seconds.
    Parameters:
      `bucketDuration` - the duration in seconds

  - ### setBucketRequests

    public void setBucketRequests(int bucketRequests)
    Set the maximum number of requests allowed per time bucket.
    Parameters:
      `bucketRequests` - the maximum number of requests

  - ### setEnforce

    public void setEnforce(boolean enforce)
    Set whether to enforce the rate limit by rejecting requests that exceed the limit.
    Parameters:
      `enforce` - `true` to enforce the rate limit, `false` to only track

  - ### setStatusCode

    public void setStatusCode(int statusCode)
    Set the HTTP status code to return when the rate limit is exceeded.
    Parameters:
      `statusCode` - the HTTP status code

  - ### setStatusMessage

    public void setStatusMessage(String statusMessage)
    Set the status message to return when the rate limit is exceeded.
    Parameters:
      `statusMessage` - the status message

  - ### setRateLimitClassName

    public void setRateLimitClassName(String rateLimitClassName)
    Set the fully qualified class name of the `RateLimiter` implementation to use.
    Parameters:
      `rateLimitClassName` - the rate limiter class name

  - ### setExposeHeaders

    public void setExposeHeaders(boolean exposeHeaders)
    Set whether to expose rate limit headers in the HTTP response.
    Parameters:
      `exposeHeaders` - `true` to expose rate limit headers

  - ### setPolicyName

    public void setPolicyName(String policyName)
    Set the name of the rate limit policy for use in response headers.
    Parameters:
      `policyName` - the policy name

  - ### isConfigProblemFatal

    protected boolean isConfigProblemFatal()
    Determines if an exception when calling a setter or an unknown configuration attribute triggers the failure of this filter which in turn will prevent the web application from starting.
    Overrides:
      `isConfigProblemFatal` in class `FilterBase`
    Returns:
      `true` if a problem should trigger the failure of this filter, else `false`

  - ### init

    public void init(FilterConfig filterConfig) throws ServletException
    Iterates over the configuration parameters and either logs a warning, or throws an exception for any parameter that does not have a matching setter in this filter.
    Specified by:
      `init` in interface `Filter`
    Overrides:
      `init` in class `FilterBase`
    Parameters:
      `filterConfig` - The configuration information associated with the filter instance being initialised
    Throws:
      `ServletException` - if `FilterBase.isConfigProblemFatal()` returns `true` and a configured parameter does not have a matching setter

  - ### doFilter

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
    The `doFilter` method of the Filter is called by the container each time a request/response pair is passed through the chain due to a client request for a resource at the end of the chain. The FilterChain passed in to this method allows the Filter to pass on the request and response to the next entity in the chain.

    A typical implementation of this method would follow the following pattern:-
    1. Examine the request
    2. Optionally wrap the request object with a custom implementation to filter content or headers for input filtering
    3. Optionally wrap the response object with a custom implementation to filter content or headers for output filtering
    4. a) Either invoke the next entity in the chain using the FilterChain object (`chain.doFilter()`),
    4. b) or not pass on the request/response pair to the next entity in the filter chain to block the request processing
    5. Directly set headers on the response after invocation of the next entity in the filter chain.

    Parameters:
      `request` - The request to process
      `response` - The response associated with the request
      `chain` - Provides access to the next filter in the chain for this filter to pass the request and response to for further processing
    Throws:
      `IOException` - if an I/O error occurs during this filter's processing of the request
      `ServletException` - if the processing fails for any other reason

  - ### destroy

    public void destroy()
    Called by the web container to indicate to a filter that it is being taken out of service. This method is only called once all threads within the filter's doFilter method have exited or after a timeout period has passed. After the web container calls this method, it will not call the doFilter method again on this instance of the filter.
    This method gives the filter an opportunity to clean up any resources that are being held (for example, memory, file handles, threads) and make sure that any persistent state is synchronized with the filter's current state in memory. The default implementation is a NO-OP.

  - ### getLogger

    protected Log getLogger()
    Returns the logger for this filter.
    Specified by:
      `getLogger` in class `FilterBase`
    Returns:
      the logger