§Allowed hosts filter

Play provides a filter that lets you configure which hosts can access your application. This is useful to prevent cache poisoning attacks. For a detailed description of how this attack works, see this blog post. The filter introduces a whitelist of allowed hosts and sends a 400 (Bad Request) response to all requests with a host that do not match the whitelist.

§Enabling the allowed hosts filter

To enable the filter, first add the Play filters project to your libraryDependencies in build.sbt:

libraryDependencies += filters

Now add the allowed hosts filter to your filters, which is typically done by creating a Filters class in the root of your project:

import javax.inject.Inject

import play.api.http.DefaultHttpFilters
import play.filters.hosts.AllowedHostsFilter

class Filters @Inject() (allowedHostsFilter: AllowedHostsFilter)
  extends DefaultHttpFilters(allowedHostsFilter)
import play.mvc.EssentialFilter;
import play.filters.hosts.AllowedHostsFilter;
import play.http.DefaultHttpFilters;

import javax.inject.Inject;

public class Filters extends DefaultHttpFilters {
    @Inject public Filters(AllowedHostsFilter allowedHostsFilter) {

§Configuring allowed hosts

You can configure which hosts the filter allows using application.conf. See the Play filters reference.conf to see the defaults.

play.filters.hosts.allowed is a list of strings of the form or With a leading dot, the pattern will match and all subdomains (,,, etc.). Without the leading dot it will just match the exact domain. If your application runs on a specific port, you can also include a port number, for instance

You can use the . pattern to match all hosts (not recommended in production). Note that the filter also strips the dot character from the end of the host, so the pattern will match

An example configuration follows.

play.filters.hosts {
  # Allow requests to, its subdomains, and localhost:9000.
  allowed = ["", "localhost:9000"]

Next: Extending Play with modules

Found an error in this documentation? The source code for this page can be found here. After reading the documentation guidelines, please feel free to contribute a pull request.