Pass the browser cors preflight with response status 204 for Method OPTIONS
Requirements: Traefik >= v2.5.5
--pilot.token=xxx--experimental.plugins.corspreflight.modulename=github.com/Medzoner/traefik-plugin-cors-preflight--experimental.plugins.corspreflight.version=v1.1.1
pilot:token: xxxexperimental:plugins:corspreflight:modulename: github.com/Medzoner/traefik-plugin-cors-preflightversion: v1.1.1
[pilot]token = "xxx"[experimental.plugins.corspreflight]modulename = "github.com/Medzoner/traefik-plugin-cors-preflight"version = "v1.1.1"
testData:testData:code: 204method: 'OPTIONS'debug: falseallowOrigins: '*'allowMethods: 'GET,POST,OPTIONS'allowHeaders: 'Content-Type,Authorization'
To configure the CorsPreflight plugin you should create a middleware in your dynamic configuration as explained here.
http:middlewares:corspreflight-middleware:plugin:corspreflight:code: 200method: OPTIONSdebug: falseallowOrigins: '*'allowMethods: 'GET,POST,OPTIONS'allowHeaders: 'Content-Type,Authorization'routers:my-router:rule: Host(`localhost`)middlewares:- corspreflight-middlewareservice: my-serviceservices:my-service:loadBalancer:servers:- url: 'http://127.0.0.1'
[http.middlewares][http.middlewares.corspreflight-middleware.plugin.corspreflight]code = 200method = "OPTIONS"debug = falseallowOrigins = "*"allowMethods = "GET,POST,OPTIONS"allowHeaders = "Content-Type,Authorization"[http.routers][http.routers.my-router]rule = "Host(`localhost`)"middlewares = ["corspreflight-middleware"]service = "my-service"[http.services][http.services.my-service][http.services.my-service.loadBalancer][[http.services.my-service.loadBalancer.servers]]url = "http://127.0.0.1"
---apiVersion: traefik.containo.us/v1alpha1kind: Middlewaremetadata:name: corspreflight-middlewarespec:plugin:corspreflight:code: 200method: OPTIONSdebug: falseallowOrigins: '*'allowMethods: 'GET,POST,OPTIONS'allowHeaders: 'Content-Type,Authorization'---apiVersion: traefik.containo.us/v1alpha1kind: IngressRoutemetadata:name: whoamispec:entryPoints:- webroutes:- kind: Rulematch: Host(`whoami.localhost`)middlewares:- name: corspreflight-middlewareservices:- kind: Servicename: whoami-svcport: 80
---apiVersion: traefik.containo.us/v1alpha1kind: Middlewaremetadata:name: corspreflight-middlewarespec:plugin:corspreflight:code: 200method: OPTIONSdebug: falseallowOrigins: '*'allowMethods: 'GET,POST,OPTIONS'allowHeaders: 'Content-Type,Authorization'---apiVersion: networking.k8s.io/v1kind: Ingressmetadata:name: myingressannotations:traefik.ingress.kubernetes.io/router.middlewares: default-corspreflight-middleware@kubernetescrdspec:rules:- host: whoami.localhosthttp:paths:- path: /pathType: Prefixbackend:service:name: whoamiport:number: 80
services:whoami:image: traefik/whoami:v1.7.1labels:traefik.enable: 'true'traefik.http.routers.app.rule: Host(`whoami.localhost`)traefik.http.routers.app.entrypoints: websecuretraefik.http.routers.app.middlewares: corspreflight-middlewaretraefik.http.middlewares.corspreflight-middleware.plugin.corspreflight.code: 204traefik.http.middlewares.corspreflight-middleware.plugin.corspreflight.method: 'OPTIONS'traefik.http.middlewares.corspreflight-middleware.plugin.corspreflight.debug: falsetraefik.http.middlewares.corspreflight-middleware.plugin.corspreflight.allowOrigins: '*'traefik.http.middlewares.corspreflight-middleware.plugin.corspreflight.allowMethods: 'GET,POST,OPTIONS'traefik.http.middlewares.corspreflight-middleware.plugin.corspreflight.allowHeaders: 'Content-Type,Authorization'