Headers
Headers
Introduction
Masonite allows you to easily add security headers to your application. Masonite adds some sensible defaults but you can modify them as you need.
Configuration
All you need to do is add the middleware to your HTTP_MIDDLEWARE
constant in your config/middleware.py
file:
This will add these default headers for your server:
Overriding Headers
If you want to change or add any headers, you just need to specify them in your config/middleware.py file and this middleware will automatically pick them up. For example you can change the X-Frame-Options
header like this:
This will then change your headers to:
Notice the change in the new header we changed.
CORS
You may also choose to use CORS for your application for advanced security measures. Using CORS is very similar to the secure headers above.
This middleware needs to be at the TOP of the HTTP_MIDDLEWARE
stack so the request will not be rejected inside the other middleware.
To get started just import the CorsProvider
class into your config/providers.py
file and add it to your PROVIDERS
list:
Then inside your config/middleware.py
file you can put your CORS headers as a dictionary. Here is a list of sensible defaults:
Now if you go to a browser you will see these headers being sent as a response from your server.
Last updated