Force referer filter ver. 1.5


This is a Java servlet filter (as per Servlet API 2.3). The idea behind this filter is very simple. Filter lets you stop downloading files from your site by users that are not visiting your site itself. For example, if someone try to download a file from your site: http://your_site/your_file.zip by click on that link on the another site: http://some_site/list_of_files.htm. In this case REFERER header in HTTP request points to some_site. And vice versa if your visitor clicks on the same link on your site REFERER header points to your site. So filter lets you set a mandatory value for REFERER header and redirect all the policy violated requests to some page.

How to use it:

a) download forcerefererflt.jar and save it in WEB-INF/lib

b) describe this filter in web.xml. Parameters are:

  referer describes a space separated list of possible mandate values for REFERER header. You can use regular expressions here
  redirect describes a page for redirection

For example:
 


<filter>
  <filter-name>ForceRefererFilter</filter-name>
  <filter-class>com.cj.refer.ForceRefererFilter</filter-class>
  <init-param>
    <param-name>referer</param-name>
    <param-value>http://myserver/license.jsp</param-value>
  </init-param>
  <init-param>
    <param-name>redirect</param-name>
    <param-value>http://myserver/license.jsp</param-value>
  </init-param>
</filter>

c) describe a mapping for this filter in web.xml
 


<filter-mapping>
  <filter-name>ForceRefererFilter</filter-name>
  <url-pattern>myfile.zip</url-pattern>
</filter-mapping>

in this case filter will be on for for file myfile.zip. So for any request to this file, like this: http://your_server/myfile.zip filter will check out referer field. And users that are not coming from license.jsp file will be redirected to that page.

Also you may exclude some of your files from the processing. Parameter exclude contains a commas separated list of files excluded from the processing. E.g.:
 


<filter>
  <filter-name>ForceRefererFilter</filter-name>
  <filter-class>com.cj.refer.ForceRefererFilter</filter-class>
  <init-param>
    <param-name>exclude</param-name>
    <param-value>/main.zip</param-value>
  </init-param>
</filter>

This filter may prevent bandwidth theft or "hotlinking" (a direct linking to your web site's files: images, video, etc.). An example would be using an <img> tag to display a JPEG image from your site on someone else's web page etc. Just describe in referer parameter your own host (and probably the search engines) and map filter to image files.

   For downloading:

    Force referer filter:  forcerefererflt.jar

 ©  Coldbeans     Comments?

See also JSOS - the largest collection of servlets and filters.

     

Also in JSOS: