Documentation

§Configuring Security Headers

Play provides a security headers filter that can be used to configure some default headers in the HTTP response to mitigate security issues and provide an extra level of defense for new applications.

§Enabling the security headers filter

Note: As of Play 2.6.x, the Security Headers filter is included in Play’s list of default filters that are applied automatically to projects. See the Filters page for more information.

To enable the security headers filter manually, add the security headers filter to your filters in application.conf:

play.filters.enabled += "play.filters.headers.SecurityHeadersFilter"

§Configuring the security headers

Scaladoc is available in the play.filters.headers package.

The filter will set headers in the HTTP response automatically. The settings can be configured through the following settings in application.conf

Any of the headers can be disabled by setting a configuration value of null, for example:

play.filters.headers.frameOptions = null

For a full listing of configuration options, see the Play filters reference.conf.

§Action-specific overrides

Security headers may be overridden in specific actions using withHeaders on the result:

	Ok("Index").withHeaders(SecurityHeadersFilter.CONTENT_SECURITY_POLICY_HEADER -> "my page-specific header")

Any security headers not mentioned in withHeaders will use the usual configured values
(if present) or the defaults. Action-specific security headers are ignored unless
play.filters.headers.allowActionSpecificHeaders is set to true in the configuration.

Next: Configuring CORS


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.