1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
|
# Ring-Defaults
[](https://travis-ci.org/ring-clojure/ring-defaults)
Knowing what middleware to add to a Ring application, and in what
order, can be difficult and prone to error.
This library attempts to automate the process, by providing sensible
and secure default configurations of Ring middleware for both websites
and HTTP APIs.
## Installation
Add the following dependency to your `project.clj`:
[ring/ring-defaults "0.3.1"]
## Basic Usage
The `wrap-defaults` middleware sets up standard Ring middleware based
on a supplied configuration:
```clojure
(require '[ring.middleware.defaults :refer :all])
(def site
(wrap-defaults handler site-defaults))
```
There are four configurations included with the middleware
- `api-defaults`
- `site-defaults`
- `secure-api-defaults`
- `secure-site-defaults`
The "api" defaults will add support for urlencoded parameters, but not
much else.
The "site" defaults add support for parameters, cookies, sessions,
static resources, file uploads, and a bunch of browser-specific
security headers.
The "secure" defaults force SSL. Unencrypted HTTP URLs are redirected
to the equivlant HTTPS URL, and various headers and flags are sent to
prevent the browser sending sensitive information over insecure
channels.
## Proxies
If your app is sitting behind a load balancer or reverse proxy, as is
often the case in cloud-based deployments, you'll want to set `:proxy`
to `true`:
```clojure
(assoc secure-site-defaults :proxy true)
```
This is particularly important when your site is secured with SSL, as
the SSL redirect middleware will get caught in a redirect loop if it
can't determine the correct URL scheme of the request.
## Customizing
The default configurations are just maps of options, and can be
customized to suit your needs. For example, if you wanted the normal
site defaults, but without session support, you could use:
```clojure
(wrap-defaults handler (assoc site-defaults :session false))
```
The following configuration keys are supported:
- `:cookies` - Set to true to parse cookies from the request.
- `:params` -
A map of options that describes how to parse parameters from the
request.
- `:keywordize` -
Set to true to turn the parameter keys into keywords.
- `:multipart` -
Set to true to parse urlencoded parameters in the query string and
the request body, or supply a map of options to pass to the
standard Ring [multipart-params][1] middleware.
- `:nested` -
Set to true to allow nested parameters via the standard Ring
[nested-params][2] middleware
- `:urlencoded` -
Set to true to parse urlencoded parameters in the query string and
the request body.
- `:proxy` -
Set to true if the application is running behind a reverse proxy or
load balancer.
- `:responses` -
A map of options to augment the responses from your application.
- `:absolute-redirects` -
Any redirects to relative URLs will be turned into redirects to
absolute URLs, to better conform to the HTTP spec.
- `:content-types` -
Adds the standard Ring [content-type][3] middleware.
- `:default-charset` -
Adds a default charset to any text content-type lacking a charset.
- `:not-modified-responses` -
Adds the standard Ring [not-modified][4] middleware.
- `:security` -
Options for security related behaviors and headers.
- `:anti-forgery` -
Set to true to add CSRF protection via the [ring-anti-forgery][5]
library.
- `:content-type-options` -
Prevents attacks based around media-type confusion. See:
[wrap-content-type-options][6].
- `:frame-options` -
Prevents your site from being placed in frames or iframes. See:
[wrap-frame-options][7].
- `:hsts` -
If true, enable HTTP Strict Transport Security. See: [wrap-hsts][8].
- `:ssl-redirect` -
If true, redirect all HTTP requests to the equivalent HTTPS URL. A
map with an `:ssl-port` option may be set instead, if the HTTPS
server is on a non-standard port. See: [wrap-ssl-redirect][9].
- `:xss-protection` -
Enable the X-XSS-Protection header that tells supporting browsers
to use heuristics to detect XSS attacks. See: [wrap-xss-protection][10].
- `:session` -
A map of options for configuring session handling via the Ring
[session][11] middleware.
- `:flash` - If set to true, the Ring [flash][12] middleware is added.
- `:store` - The Ring session store to use for storing sessions.
- `:static`
A map of options to configure how to find static content.
- `:files` -
A string or collection of strings containing paths to directories
to serve files from. Usually the `:resources` option below is
more useful.
- `:resources` -
A string or collection of strings containing classpath
prefixes. This will serve any resources in locations starting with
the supplied prefix.
[1]: https://ring-clojure.github.io/ring/ring.middleware.multipart-params.html
[2]: https://ring-clojure.github.io/ring/ring.middleware.nested-params.html
[3]: https://ring-clojure.github.io/ring/ring.middleware.content-type.html
[4]: https://ring-clojure.github.io/ring/ring.middleware.not-modified.html
[5]: https://github.com/ring-clojure/ring-anti-forgery
[6]: https://ring-clojure.github.io/ring-headers/ring.middleware.x-headers.html#var-wrap-content-type-options
[7]: https://ring-clojure.github.io/ring-headers/ring.middleware.x-headers.html#var-wrap-frame-options
[8]: https://ring-clojure.github.io/ring-ssl/ring.middleware.ssl.html#var-wrap-hsts
[9]: https://ring-clojure.github.io/ring-ssl/ring.middleware.ssl.html#var-wrap-ssl-redirect
[10]: https://ring-clojure.github.io/ring-headers/ring.middleware.x-headers.html#var-wrap-xss-protection
[11]: https://ring-clojure.github.io/ring/ring.middleware.session.html
[12]: https://ring-clojure.github.io/ring/ring.middleware.flash.html
## License
Copyright © 2017 James Reeves
Distributed under the MIT License, the same as Ring.
|