CORS Filter

Configuration

The CORS Filter can be run with no additional configuration in most situations. By default it operates in public mode, informing the browser that:

If the default public CORS mode doesn't satisfy your requirements, you can override it by specifying your own configuration. The following three configuration methods are supported:

The CORS Filter applies the following precedence when resolving the configuration properties:

  1. Checks for a cors.configurationFile environment variable and if it's defined loads the properties file from the referenced location (typically relative to the web application root).
  2. Checks for a filter init-param cors.configurationFile and if it's defined loads the properties file from the referenced location (typically relative to the web application root).
  3. Checks for the CORS configuration properties in the filter init-param section and if a property is not defined applies the default value.

For example, here a a web.xml snippet to allow CORS requests from the http://example.com origin only:

<filter>
        <filter-name>CORS</filter-name>
        <filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>

        <init-param>
                <param-name>cors.allowOrigin</param-name>
                <param-value>http://example.com</param-value>
        </init-param>
</filter>

Or as an external properties file, referenced by the cors.configurationFile environment variable or filter init-param:

cors.allowOrigin = http://example.com

Look at the web.xml of the demo CORS application included with the download package to see a complete CORS filter declaration, configuration and mapping example.

Configuration parameters

This is a description of the CORS Filter configuration properties.

Remember to restart your web application or server after making CORS configuration changes!