2022-04-15 13:44:08 +00:00
---
title: "Traefik ForwardAuth Documentation"
description: "In Traefik Proxy, the HTTP ForwardAuth middleware delegates authentication to an external Service. Read the technical documentation."
---
2019-02-26 13:50:07 +00:00
# ForwardAuth
2021-02-11 13:34:04 +00:00
Using an External Service to Forward Authentication
2019-02-26 13:50:07 +00:00
{: .subtitle }
2021-06-11 13:30:05 +00:00
![AuthForward ](../../assets/img/middleware/authforward.png )
2019-02-26 13:50:07 +00:00
2021-02-11 13:34:04 +00:00
The ForwardAuth middleware delegates authentication to an external service.
If the service answers with a 2XX code, access is granted, and the original request is performed.
2019-02-26 13:50:07 +00:00
Otherwise, the response from the authentication server is returned.
## Configuration Examples
2023-05-10 13:28:05 +00:00
```yaml tab="Docker & Swarm"
2020-03-13 21:50:05 +00:00
# Forward authentication to example.com
2019-03-29 11:34:05 +00:00
labels:
2020-03-13 21:50:05 +00:00
- "traefik.http.middlewares.test-auth.forwardauth.address=https://example.com/auth"
2019-09-05 11:42:04 +00:00
```
```yaml tab="Kubernetes"
2020-03-13 21:50:05 +00:00
# Forward authentication to example.com
2023-03-20 14:38:08 +00:00
apiVersion: traefik.io/v1alpha1
2019-09-05 11:42:04 +00:00
kind: Middleware
metadata:
name: test-auth
spec:
forwardAuth:
2020-03-13 21:50:05 +00:00
address: https://example.com/auth
2019-09-05 11:42:04 +00:00
```
2019-10-15 15:34:08 +00:00
```yaml tab="Consul Catalog"
2020-03-13 21:50:05 +00:00
# Forward authentication to example.com
- "traefik.http.middlewares.test-auth.forwardauth.address=https://example.com/auth"
2019-10-15 15:34:08 +00:00
```
2019-09-05 11:42:04 +00:00
```yaml tab="File (YAML)"
2020-03-13 21:50:05 +00:00
# Forward authentication to example.com
2019-09-05 11:42:04 +00:00
http:
middlewares:
test-auth:
forwardAuth:
2020-03-13 21:50:05 +00:00
address: "https://example.com/auth"
2019-09-05 11:42:04 +00:00
```
2021-06-18 22:08:08 +00:00
```toml tab="File (TOML)"
# Forward authentication to example.com
[http.middlewares]
[http.middlewares.test-auth.forwardAuth]
address = "https://example.com/auth"
```
2020-11-10 16:50:04 +00:00
## Forward-Request Headers
The following request properties are provided to the forward-auth target endpoint as `X-Forwarded-` headers.
| Property | Forward-Request Header |
|-------------------|------------------------|
| HTTP Method | X-Forwarded-Method |
| Protocol | X-Forwarded-Proto |
| Host | X-Forwarded-Host |
| Request URI | X-Forwarded-Uri |
| Source IP-Address | X-Forwarded-For |
2019-09-05 11:42:04 +00:00
## Configuration Options
### `address`
The `address` option defines the authentication server address.
2023-05-10 13:28:05 +00:00
```yaml tab="Docker & Swarm"
2019-09-05 11:42:04 +00:00
labels:
2020-03-13 21:50:05 +00:00
- "traefik.http.middlewares.test-auth.forwardauth.address=https://example.com/auth"
2019-09-05 11:42:04 +00:00
```
```yaml tab="Kubernetes"
2023-03-20 14:38:08 +00:00
apiVersion: traefik.io/v1alpha1
2019-09-05 11:42:04 +00:00
kind: Middleware
metadata:
name: test-auth
spec:
forwardAuth:
2021-06-18 22:08:08 +00:00
address: https://example.com/auth
2019-09-05 11:42:04 +00:00
```
2019-10-15 15:34:08 +00:00
```yaml tab="Consul Catalog"
2020-03-13 21:50:05 +00:00
- "traefik.http.middlewares.test-auth.forwardauth.address=https://example.com/auth"
2019-10-15 15:34:08 +00:00
```
2019-09-05 11:42:04 +00:00
```yaml tab="File (YAML)"
http:
middlewares:
test-auth:
forwardAuth:
2020-03-13 21:50:05 +00:00
address: "https://example.com/auth"
2019-09-05 11:42:04 +00:00
```
2021-06-18 22:08:08 +00:00
```toml tab="File (TOML)"
[http.middlewares]
[http.middlewares.test-auth.forwardAuth]
address = "https://example.com/auth"
```
2019-09-05 11:42:04 +00:00
### `trustForwardHeader`
2021-02-11 13:34:04 +00:00
Set the `trustForwardHeader` option to `true` to trust all `X-Forwarded-*` headers.
2019-09-05 11:42:04 +00:00
2023-05-10 13:28:05 +00:00
```yaml tab="Docker & Swarm"
2019-09-05 11:42:04 +00:00
labels:
2019-09-23 15:00:06 +00:00
- "traefik.http.middlewares.test-auth.forwardauth.trustForwardHeader=true"
2019-03-29 11:34:05 +00:00
```
2019-02-26 13:50:07 +00:00
2019-04-03 12:32:04 +00:00
```yaml tab="Kubernetes"
2023-03-20 14:38:08 +00:00
apiVersion: traefik.io/v1alpha1
2019-04-03 12:32:04 +00:00
kind: Middleware
metadata:
name: test-auth
spec:
forwardAuth:
2020-03-13 21:50:05 +00:00
address: https://example.com/auth
2019-04-03 12:32:04 +00:00
trustForwardHeader: true
2019-09-05 11:42:04 +00:00
```
2019-10-15 15:34:08 +00:00
```yaml tab="Consul Catalog"
- "traefik.http.middlewares.test-auth.forwardauth.trustForwardHeader=true"
```
2019-09-05 11:42:04 +00:00
```yaml tab="File (YAML)"
http:
middlewares:
test-auth:
forwardAuth:
2020-03-13 21:50:05 +00:00
address: "https://example.com/auth"
2019-09-05 11:42:04 +00:00
trustForwardHeader: true
```
2021-06-18 22:08:08 +00:00
```toml tab="File (TOML)"
[http.middlewares]
[http.middlewares.test-auth.forwardAuth]
address = "https://example.com/auth"
trustForwardHeader = true
```
2019-09-05 11:42:04 +00:00
### `authResponseHeaders`
2021-02-11 13:34:04 +00:00
The `authResponseHeaders` option is the list of headers to copy from the authentication server response and set on
forwarded request, replacing any existing conflicting headers.
2019-09-05 11:42:04 +00:00
2023-05-10 13:28:05 +00:00
```yaml tab="Docker & Swarm"
2019-09-05 11:42:04 +00:00
labels:
2019-09-23 15:00:06 +00:00
- "traefik.http.middlewares.test-auth.forwardauth.authResponseHeaders=X-Auth-User, X-Secret"
2019-09-05 11:42:04 +00:00
```
```yaml tab="Kubernetes"
2023-03-20 14:38:08 +00:00
apiVersion: traefik.io/v1alpha1
2019-09-05 11:42:04 +00:00
kind: Middleware
metadata:
name: test-auth
spec:
forwardAuth:
2020-03-13 21:50:05 +00:00
address: https://example.com/auth
2019-04-03 12:32:04 +00:00
authResponseHeaders:
2019-09-23 15:00:06 +00:00
- X-Auth-User
- X-Secret
2019-04-03 12:32:04 +00:00
```
2019-10-15 15:34:08 +00:00
```yaml tab="Consul Catalog"
- "traefik.http.middlewares.test-auth.forwardauth.authResponseHeaders=X-Auth-User, X-Secret"
```
2019-09-05 11:42:04 +00:00
```yaml tab="File (YAML)"
http:
middlewares:
test-auth:
forwardAuth:
2020-03-13 21:50:05 +00:00
address: "https://example.com/auth"
2019-09-05 11:42:04 +00:00
authResponseHeaders:
2019-09-23 15:00:06 +00:00
- "X-Auth-User"
- "X-Secret"
2019-09-05 11:42:04 +00:00
```
2021-06-18 22:08:08 +00:00
```toml tab="File (TOML)"
[http.middlewares]
[http.middlewares.test-auth.forwardAuth]
address = "https://example.com/auth"
authResponseHeaders = ["X-Auth-User", "X-Secret"]
```
2020-10-29 14:10:04 +00:00
### `authResponseHeadersRegex`
2021-02-11 13:34:04 +00:00
The `authResponseHeadersRegex` option is the regex to match headers to copy from the authentication server response and
set on forwarded request, after stripping all headers that match the regex.
It allows partial matching of the regular expression against the header key.
The start of string (`^`) and end of string (`$`) anchors should be used to ensure a full match against the header key.
2020-10-29 14:10:04 +00:00
2023-05-10 13:28:05 +00:00
```yaml tab="Docker & Swarm"
2020-10-29 14:10:04 +00:00
labels:
- "traefik.http.middlewares.test-auth.forwardauth.authResponseHeadersRegex=^X-"
```
```yaml tab="Kubernetes"
2023-03-20 14:38:08 +00:00
apiVersion: traefik.io/v1alpha1
2020-10-29 14:10:04 +00:00
kind: Middleware
metadata:
name: test-auth
spec:
forwardAuth:
address: https://example.com/auth
authResponseHeadersRegex: ^X-
```
```yaml tab="Consul Catalog"
- "traefik.http.middlewares.test-auth.forwardauth.authResponseHeadersRegex=^X-"
```
```yaml tab="File (YAML)"
http:
middlewares:
test-auth:
forwardAuth:
address: "https://example.com/auth"
authResponseHeadersRegex: "^X-"
```
2021-06-18 22:08:08 +00:00
```toml tab="File (TOML)"
[http.middlewares]
[http.middlewares.test-auth.forwardAuth]
address = "https://example.com/auth"
authResponseHeadersRegex = "^X-"
```
2021-10-06 09:36:11 +00:00
!!! tip
Regular expressions and replacements can be tested using online tools such as [Go Playground ](https://play.golang.org/p/mWU9p-wk2ru ) or the [Regex101 ](https://regex101.com/r/58sIgx/2 ).
When defining a regular expression within YAML, any escaped character needs to be escaped twice: `example\.com` needs to be written as `example\\.com` .
2020-10-07 14:36:04 +00:00
### `authRequestHeaders`
The `authRequestHeaders` option is the list of the headers to copy from the request to the authentication server.
2020-12-22 14:36:03 +00:00
It allows filtering headers that should not be passed to the authentication server.
2021-02-11 13:34:04 +00:00
If not set or empty then all request headers are passed.
2020-10-07 14:36:04 +00:00
2023-05-10 13:28:05 +00:00
```yaml tab="Docker & Swarm"
2020-10-07 14:36:04 +00:00
labels:
- "traefik.http.middlewares.test-auth.forwardauth.authRequestHeaders=Accept,X-CustomHeader"
```
```yaml tab="Kubernetes"
2023-03-20 14:38:08 +00:00
apiVersion: traefik.io/v1alpha1
2020-10-07 14:36:04 +00:00
kind: Middleware
metadata:
name: test-auth
spec:
forwardAuth:
address: https://example.com/auth
authRequestHeaders:
- "Accept"
- "X-CustomHeader"
```
```yaml tab="Consul Catalog"
- "traefik.http.middlewares.test-auth.forwardauth.authRequestHeaders=Accept,X-CustomHeader"
```
```yaml tab="File (YAML)"
http:
middlewares:
test-auth:
forwardAuth:
address: "https://example.com/auth"
authRequestHeaders:
- "Accept"
- "X-CustomHeader"
```
2021-06-18 22:08:08 +00:00
```toml tab="File (TOML)"
[http.middlewares]
[http.middlewares.test-auth.forwardAuth]
address = "https://example.com/auth"
authRequestHeaders = "Accept,X-CustomHeader"
```
2024-01-15 15:14:05 +00:00
### `addAuthCookiesToResponse`
The `addAuthCookiesToResponse` option is the list of cookies to copy from the authentication server to the response,
replacing any existing conflicting cookie from the forwarded response.
!!! info
Please note that all backend cookies matching the configured list will not be added to the response.
```yaml tab="Docker"
labels:
- "traefik.http.middlewares.test-auth.forwardauth.addAuthCookiesToResponse=Session-Cookie,State-Cookie"
```
```yaml tab="Kubernetes"
2024-05-15 13:52:04 +00:00
apiVersion: traefik.io/v1alpha1
2024-01-15 15:14:05 +00:00
kind: Middleware
metadata:
name: test-auth
spec:
forwardAuth:
address: https://example.com/auth
addAuthCookiesToResponse:
- Session-Cookie
- State-Cookie
```
```yaml tab="Consul Catalog"
- "traefik.http.middlewares.test-auth.forwardauth.addAuthCookiesToResponse=Session-Cookie,State-Cookie"
```
```yaml tab="File (YAML)"
http:
middlewares:
test-auth:
forwardAuth:
address: "https://example.com/auth"
addAuthCookiesToResponse:
- "Session-Cookie"
- "State-Cookie"
```
2024-05-15 13:52:04 +00:00
```toml tab="File (TOML)"
[http.middlewares]
[http.middlewares.test-auth.forwardAuth]
address = "https://example.com/auth"
addAuthCookiesToResponse = ["Session-Cookie", "State-Cookie"]
```
2019-09-05 11:42:04 +00:00
### `tls`
2021-12-02 14:42:06 +00:00
_Optional_
2019-09-05 11:42:04 +00:00
2021-12-02 14:42:06 +00:00
Defines the TLS configuration used for the secure connection to the authentication server.
2019-09-05 11:42:04 +00:00
2021-12-02 14:42:06 +00:00
#### `ca`
_Optional_
`ca` is the path to the certificate authority used for the secured connection to the authentication server,
it defaults to the system bundle.
2019-09-05 11:42:04 +00:00
2023-05-10 13:28:05 +00:00
```yaml tab="Docker & Swarm"
2019-09-05 11:42:04 +00:00
labels:
2019-09-23 15:00:06 +00:00
- "traefik.http.middlewares.test-auth.forwardauth.tls.ca=path/to/local.crt"
2019-09-05 11:42:04 +00:00
```
```yaml tab="Kubernetes"
2023-03-20 14:38:08 +00:00
apiVersion: traefik.io/v1alpha1
2019-09-05 11:42:04 +00:00
kind: Middleware
metadata:
name: test-auth
spec:
forwardAuth:
2020-03-13 21:50:05 +00:00
address: https://example.com/auth
2019-09-05 11:42:04 +00:00
tls:
caSecret: mycasercret
---
apiVersion: v1
kind: Secret
metadata:
name: mycasercret
namespace: default
data:
2021-06-14 16:06:10 +00:00
# Must contain a certificate under either a `tls.ca` or a `ca.crt` key.
tls.ca: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCi0tLS0tRU5EIENFUlRJRklDQVRFLS0tLS0=
2019-09-05 11:42:04 +00:00
```
2019-10-15 15:34:08 +00:00
```yaml tab="Consul Catalog"
- "traefik.http.middlewares.test-auth.forwardauth.tls.ca=path/to/local.crt"
```
2019-09-05 11:42:04 +00:00
```yaml tab="File (YAML)"
http:
middlewares:
test-auth:
forwardAuth:
2020-03-13 21:50:05 +00:00
address: "https://example.com/auth"
2019-09-05 11:42:04 +00:00
tls:
ca: "path/to/local.crt"
```
2021-06-18 22:08:08 +00:00
```toml tab="File (TOML)"
[http.middlewares]
[http.middlewares.test-auth.forwardAuth]
address = "https://example.com/auth"
[http.middlewares.test-auth.forwardAuth.tls]
ca = "path/to/local.crt"
```
2021-12-02 14:42:06 +00:00
#### `cert`
2019-09-05 11:42:04 +00:00
2021-12-02 14:42:06 +00:00
_Optional_
`cert` is the path to the public certificate used for the secure connection to the authentication server.
When using this option, setting the `key` option is required.
2019-09-05 11:42:04 +00:00
2023-05-10 13:28:05 +00:00
```yaml tab="Docker & Swarm"
2019-09-05 11:42:04 +00:00
labels:
2019-09-23 15:00:06 +00:00
- "traefik.http.middlewares.test-auth.forwardauth.tls.cert=path/to/foo.cert"
- "traefik.http.middlewares.test-auth.forwardauth.tls.key=path/to/foo.key"
2019-09-05 11:42:04 +00:00
```
```yaml tab="Kubernetes"
2023-03-20 14:38:08 +00:00
apiVersion: traefik.io/v1alpha1
2019-09-05 11:42:04 +00:00
kind: Middleware
metadata:
name: test-auth
spec:
forwardAuth:
2020-03-13 21:50:05 +00:00
address: https://example.com/auth
2019-09-05 11:42:04 +00:00
tls:
certSecret: mytlscert
---
apiVersion: v1
kind: Secret
metadata:
name: mytlscert
namespace: default
data:
tls.crt: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCi0tLS0tRU5EIENFUlRJRklDQVRFLS0tLS0=
tls.key: LS0tLS1CRUdJTiBQUklWQVRFIEtFWS0tLS0tCi0tLS0tRU5EIFBSSVZBVEUgS0VZLS0tLS0=
```
2019-10-15 15:34:08 +00:00
```yaml tab="Consul Catalog"
- "traefik.http.middlewares.test-auth.forwardauth.tls.cert=path/to/foo.cert"
- "traefik.http.middlewares.test-auth.forwardauth.tls.key=path/to/foo.key"
```
2019-09-05 11:42:04 +00:00
```yaml tab="File (YAML)"
http:
middlewares:
test-auth:
forwardAuth:
2020-03-13 21:50:05 +00:00
address: "https://example.com/auth"
2019-09-05 11:42:04 +00:00
tls:
cert: "path/to/foo.cert"
key: "path/to/foo.key"
```
2021-06-18 22:08:08 +00:00
```toml tab="File (TOML)"
[http.middlewares]
[http.middlewares.test-auth.forwardAuth]
address = "https://example.com/auth"
[http.middlewares.test-auth.forwardAuth.tls]
cert = "path/to/foo.cert"
key = "path/to/foo.key"
```
2019-09-23 12:32:04 +00:00
!!! info
2021-02-11 13:34:04 +00:00
For security reasons, the field does not exist for Kubernetes IngressRoute, and one should use the `secret` field instead.
2019-04-03 12:32:04 +00:00
2021-12-02 14:42:06 +00:00
#### `key`
_Optional_
2019-09-05 11:42:04 +00:00
2021-12-02 14:42:06 +00:00
`key` is the path to the private key used for the secure connection to the authentication server.
When using this option, setting the `cert` option is required.
2019-09-05 11:42:04 +00:00
2023-05-10 13:28:05 +00:00
```yaml tab="Docker & Swarm"
2019-09-05 11:42:04 +00:00
labels:
2019-09-23 15:00:06 +00:00
- "traefik.http.middlewares.test-auth.forwardauth.tls.cert=path/to/foo.cert"
- "traefik.http.middlewares.test-auth.forwardauth.tls.key=path/to/foo.key"
2019-09-05 11:42:04 +00:00
```
```yaml tab="Kubernetes"
2023-03-20 14:38:08 +00:00
apiVersion: traefik.io/v1alpha1
2019-09-05 11:42:04 +00:00
kind: Middleware
metadata:
name: test-auth
spec:
forwardAuth:
2020-03-13 21:50:05 +00:00
address: https://example.com/auth
2019-09-05 11:42:04 +00:00
tls:
certSecret: mytlscert
---
apiVersion: v1
kind: Secret
metadata:
name: mytlscert
namespace: default
data:
tls.crt: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCi0tLS0tRU5EIENFUlRJRklDQVRFLS0tLS0=
tls.key: LS0tLS1CRUdJTiBQUklWQVRFIEtFWS0tLS0tCi0tLS0tRU5EIFBSSVZBVEUgS0VZLS0tLS0=
```
2019-10-15 15:34:08 +00:00
```yaml tab="Consul Catalog"
- "traefik.http.middlewares.test-auth.forwardauth.tls.cert=path/to/foo.cert"
- "traefik.http.middlewares.test-auth.forwardauth.tls.key=path/to/foo.key"
```
2019-07-22 07:58:04 +00:00
```yaml tab="File (YAML)"
http:
middlewares:
test-auth:
forwardAuth:
2020-03-13 21:50:05 +00:00
address: "https://example.com/auth"
2019-07-22 07:58:04 +00:00
tls:
cert: "path/to/foo.cert"
key: "path/to/foo.key"
```
2021-06-18 22:08:08 +00:00
```toml tab="File (TOML)"
[http.middlewares]
[http.middlewares.test-auth.forwardAuth]
address = "https://example.com/auth"
[http.middlewares.test-auth.forwardAuth.tls]
cert = "path/to/foo.cert"
key = "path/to/foo.key"
```
2019-09-23 12:32:04 +00:00
!!! info
2021-02-11 13:34:04 +00:00
For security reasons, the field does not exist for Kubernetes IngressRoute, and one should use the `secret` field instead.
2019-02-26 13:50:07 +00:00
2021-12-02 14:42:06 +00:00
#### `insecureSkipVerify`
_Optional, Default=false_
2019-02-26 13:50:07 +00:00
2021-02-11 13:34:04 +00:00
If `insecureSkipVerify` is `true` , the TLS connection to the authentication server accepts any certificate presented by the server regardless of the hostnames it covers.
2019-02-26 13:50:07 +00:00
2023-05-10 13:28:05 +00:00
```yaml tab="Docker & Swarm"
2019-09-05 11:42:04 +00:00
labels:
2019-09-23 15:00:06 +00:00
- "traefik.http.middlewares.test-auth.forwardauth.tls.insecureSkipVerify=true"
2019-09-05 11:42:04 +00:00
```
2019-02-26 13:50:07 +00:00
2019-09-05 11:42:04 +00:00
```yaml tab="Kubernetes"
2023-03-20 14:38:08 +00:00
apiVersion: traefik.io/v1alpha1
2019-09-05 11:42:04 +00:00
kind: Middleware
metadata:
name: test-auth
spec:
forwardAuth:
2020-03-13 21:50:05 +00:00
address: https://example.com/auth
2019-10-18 09:50:05 +00:00
tls:
insecureSkipVerify: true
2019-09-05 11:42:04 +00:00
```
2019-02-26 13:50:07 +00:00
2019-10-15 15:34:08 +00:00
```yaml tab="Consul Catalog"
- "traefik.http.middlewares.test-auth.forwardauth.tls.InsecureSkipVerify=true"
```
2019-09-05 11:42:04 +00:00
```yaml tab="File (YAML)"
http:
middlewares:
test-auth:
forwardAuth:
2020-03-13 21:50:05 +00:00
address: "https://example.com/auth"
2019-10-18 09:50:05 +00:00
tls:
insecureSkipVerify: true
2019-09-05 11:42:04 +00:00
```
2021-06-18 22:08:08 +00:00
```toml tab="File (TOML)"
[http.middlewares]
[http.middlewares.test-auth.forwardAuth]
address = "https://example.com/auth"
[http.middlewares.test-auth.forwardAuth.tls]
insecureSkipVerify: true
```
2024-07-29 12:30:05 +00:00
### `headerField`
_Optional_
You can define a header field to store the authenticated user using the `headerField` option.
```yaml tab="Docker & Swarm"
labels:
- "traefik.http.middlewares.test-auth.forwardauth.headerField=X-WebAuth-User"
```
```yaml tab="Kubernetes"
apiVersion: traefik.io/v1alpha1
kind: Middleware
metadata:
name: test-auth
spec:
forwardAuth:
# ...
headerField: X-WebAuth-User
```
```json tab="Consul Catalog"
- "traefik.http.middlewares.test-auth.forwardauth.headerField=X-WebAuth-User"
```
```yaml tab="File (YAML)"
http:
middlewares:
test-auth:
forwardAuth:
# ...
headerField: "X-WebAuth-User"
```
```toml tab="File (TOML)"
[http.middlewares.test-auth.forwardAuth]
# ...
headerField = "X-WebAuth-User"
```