Magnolia 5.7 reached extended end of life on May 31, 2022. Support for this branch is limited, see End-of-life policy. Please note that to cover the extra maintenance effort, this EEoL period is a paid extension in the life of the branch. Customers who opt for the extended maintenance will need a new license key to run future versions of Magnolia 5.7. If you have any questions or to subscribe to the extended maintenance, please get in touch with your local contact at Magnolia.
This page explains how to create and configure a custom Filter.
To ensure your filter is properly registered using Magnolia configuration, your filter should implement
The example above is a dummy example inspired by GNU Terry Pratchett HTTP-Header.
Configuration and Registration of filters
Filters in a Java webapp using servlets 2.5 must be registered in the
When adding a filter to Magnolia - you should not register it in web.xml but on Magnolia configuration. If the configuration is done properly - filters are registered dynamically by Magnolia.
You can configure filters implementing
As with any other configuration data - bootstrap the configuration within your module with a bootstrap file or create an installation task to ensure configuration is set correctly when your project must be reinstalled or deployed.
Add a node to
To configure a filter add a node to /server/filters:
The fully qualified class name of your custom filter (which must implement
optional, default is
Setting to false disables the filter.
Configuring and registering a filter which does not implement info.magnolia.cms.filters.MgnlFilter
If you have a filter which can not implement
In this case class must be info.magnolia.cms.filters.FilterDecorator.
The fully qualified class name of your custom filter (which does not implement
A map to add filter init parameters. You may add as many parameters as required. The init parameters can be read in the init method of the filter (see below).
Adding filter init parameters
javax.servlet.Filter has an #init method which is called once when the filter is initialized.
Init parameters for filters implementing info.magnolia.cms.filters.MgnlFilter
When using filters implementing
|Klacker messages are save and reliable.|
Init parameters for filters not implementing info.magnolia.cms.filters.MgnlFilter
If you are using FilterDecorator for custom filters not implementing
Ensure position in filter chain
Filters are executed in the order they are in the chain, from top to bottom. If you do not care about execution order, you can skip this section, otherwise make sure the filter is in the right position in the chain.
Magnolia provides task classes to adapt the order of the filter via module version handler. Use FilterOrderingTask or OrderFilterBeforeTask. The code example below is using the
When installing a module, you can define the position with
The code above would ensure to have the filter "klacker" right after the filter "contentType".