@@ -362,6 +362,8 @@ You have to substitute *name* with the actual cookie name you configured via --c
362362
363363**This option requires `--reverse-proxy` option to be set.**
364364
365+### ForwardAuth with 401 errors middleware
366+
365367The [Traefik v2 `ForwardAuth` middleware](https://doc.traefik.io/traefik/middlewares/forwardauth/) allows Traefik to authenticate requests via the oauth2-proxy's `/oauth2/auth` endpoint on every request, which only returns a 202 Accepted response or a 401 Unauthorized response without proxying the whole request through. For example, on Dynamic File (YAML) Configuration:
366368
367369```yaml
@@ -425,6 +427,104 @@ http:
425427 query: "/oauth2/sign_in"
426428```
427429
430+### ForwardAuth with static upstreams configuration
431+
432+Redirect to sign_in functionality provided without the use of `errors` middleware with [Traefik v2 `ForwardAuth` middleware](https://doc.traefik.io/traefik/middlewares/forwardauth/) pointing to oauth2-proxy service's `/` endpoint
433+
434+**Following options need to be set on `oauth2-proxy`:**
435+- `--upstream=static://202`: Configures a static response for authenticated sessions
436+- `--reverseproxy=true`: Enables the use of `X-Forwarded-*` headers to determine redirects correctly
437+
438+```yaml
439+http:
440+ routers:
441+ a-service-route-1:
442+ rule: "Host(`a-service.example.com`, `b-service.example.com`) && PathPrefix(`/`)"
443+ service: a-service-backend
444+ middlewares:
445+ - oauth-auth-redirect # redirects all unauthenticated to oauth2 signin
446+ tls:
447+ certResolver: default
448+ domains:
449+ - main: "example.com"
450+ sans:
451+ - "*.example.com"
452+ a-service-route-2:
453+ rule: "Host(`a-service.example.com`) && PathPrefix(`/no-auto-redirect`)"
454+ service: a-service-backend
455+ middlewares:
456+ - oauth-auth-wo-redirect # unauthenticated session will return a 401
457+ tls:
458+ certResolver: default
459+ domains:
460+ - main: "example.com"
461+ sans:
462+ - "*.example.com"
463+ services-oauth2-route:
464+ rule: "Host(`a-service.example.com`, `b-service.example.com`) && PathPrefix(`/oauth2/`)"
465+ middlewares:
466+ - auth-headers
467+ service: oauth-backend
468+ tls:
469+ certResolver: default
470+ domains:
471+ - main: "example.com"
472+ sans:
473+ - "*.example.com"
474+ oauth2-proxy-route:
475+ rule: "Host(`oauth.example.com`) && PathPrefix(`/`)"
476+ middlewares:
477+ - auth-headers
478+ service: oauth-backend
479+ tls:
480+ certResolver: default
481+ domains:
482+ - main: "example.com"
483+ sans:
484+ - "*.example.com"
485+
486+ services:
487+ a-service-backend:
488+ loadBalancer:
489+ servers:
490+ - url: http://172.16.0.2:7555
491+ b-service-backend:
492+ loadBalancer:
493+ servers:
494+ - url: http://172.16.0.3:7555
495+ oauth-backend:
496+ loadBalancer:
497+ servers:
498+ - url: http://172.16.0.1:4180
499+
500+ middlewares:
501+ auth-headers:
502+ headers:
503+ sslRedirect: true
504+ stsSeconds: 315360000
505+ browserXssFilter: true
506+ contentTypeNosniff: true
507+ forceSTSHeader: true
508+ sslHost: example.com
509+ stsIncludeSubdomains: true
510+ stsPreload: true
511+ frameDeny: true
512+ oauth-auth-redirect:
513+ forwardAuth:
514+ address: https://oauth.example.com/
515+ trustForwardHeader: true
516+ authResponseHeaders:
517+ - X-Auth-Request-Access-Token
518+ - Authorization
519+ oauth-auth-wo-redirect:
520+ forwardAuth:
521+ address: https://oauth.example.com/oauth2/auth
522+ trustForwardHeader: true
523+ authResponseHeaders:
524+ - X-Auth-Request-Access-Token
525+ - Authorization
526+```
527+
428528:::note
429529If you set up your OAuth2 provider to rotate your client secret, you can use the `client-secret-file` option to reload the secret when it is updated.
430530:::