Data AccessCore JavaApp FrameworksViewTestingBuildingDeploymentDev ToolsSecurityOpinions

Thursday, December 4, 2008

Filtering IP traffic using a Java Filter - RemoteAddrFilter

I recently wrote a Java Filter, which is a replica of the org.apache.catalina.valves.RemoteAddrValve implementation. It performs filtering based on comparing the requestors remote IP address against a set of regular expressions, configured in the Filter's initialization parameters. If an IP addresses is to be rejected it is rejected with a Forbidden HTTP response.

This provided useful because Valves are attached to the servlet container while a Filter can be mapped to any url pattern at the application level rather than at the container level.

GET THE CODE
Here is a link to the filter code. (RemoteAddrFilter.java)

This filter is configured by setting the allow and/or deny properties to a comma-delimited list of regular expressions to which the requestors remote address will be compared. Evaluation proceeds as follows:
  • The filter initializes reading the allow and/or deny properties and converting them to a comma-delimited list of regular expressions to which the requester's remote address will be compared.
  • If there are any deny expressions configured, the property will be compared to each such expression. If a match is found, this request will be rejected with a "Forbidden" HTTP response.
  • If there are any allow expressions configured, the property will be compared to each such expression. If a match is found, this request will be allowed to pass through to the next Filter in the current pipeline.
  • If one or more deny expressions was specified but no allow expressions, allow this request to pass through (because none of the deny expressions matched it).
  • The request will be rejected with a "Forbidden" HTTP response.
The filter is configured the same way all Java Filter's. This filter takes two initial parameters. The value of those properties should be set to a comma-delimited list of regular expressions to which the requestors remote address will be compared.

CONFIGURE
The filter is added to your context via the web.xml. Below is an example configuration.

<filter>
   <filter-name>RemoteAddrFilter</filter-name>
   <filter-class>RemoteAddrFilter<filter-class>
   <init-param>
      <param-name>allow</param-name>
      <param-value>192.168.1.*</param-value>
   </init-param>
   <init-param>
      <param-name>deny</param-name>
      <param-value>163.122.111.*</param-value>
   </init-param>
</filter>


DEPENDENCIES
There is a dependency with Jakarta Regexp.

6 comments:

  1. Entrepreneurs generally should switch the Mastercard processor in light of the fact that the progress is paid back naturally as a level of each cluster's returns, yet the rates will be the same if worse.cash advance

    ReplyDelete
  2. Before beginning my business I was a selective purchaser from the auxiliary tickets advertise, which means I never purchased "confront esteem" since I needed the best seats in the house, a self broadcasted "Ticket Location Snob" (TLS). boston red sox tickets

    ReplyDelete
  3. Looking for professional and licensed verified Packers and Movers at an affordable cost?
    Your search ends here... Hire the most licensed and verified packers and movers at an affordable cost and save money, compare the quotes with the top 3 competitors and choose the best one!
    100% Licensed & verified Transporters in India

    Packers and Movers India | Licensed Transporters | Hassle-free relocation
    Call now : 8886663106

    Packers and Movers in Hyderabad
    Packers and Movers in Bangalore
    Best Packers and Movers in Vizag
    Packers and Movers Kolkata


    ReplyDelete