我知道我的问题似乎已经有了很多答案,但请耐心等待。
我有一个在k8s集群上运行的Django应用程序,其nginx入口设置带有letencrypt临时tls证书(该问题也出现在生产证书中)。
我不知道这是否重要,但是应用程序使用Basic Authentication
内置身份验证系统设置的drf
来授权用户。
我已经按照以下步骤设置了CORS:
### ingress.yaml
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: app-routing
annotations:
kubernetes.io/ingress.class: "nginx"
cert-manager.io/cluster-issuer: "letsencrypt-staging"
nginx.ingress.kubernetes.io/enable-cors: "true"
nginx.ingress.kubernetes.io/cors-allow-methods: "PUT, GET, POST, PATCH, OPTIONS"
nginx.ingress.kubernetes.io/cors-allow-origin: "https://example.com"
nginx.ingress.kubernetes.io/cors-allow-credentials: "true"
nginx.ingress.kubernetes.io/cors-allow-headers: "DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization,X-CSRFToken"
spec:
tls:
- hosts:
- api.example.com
secretName: app-ingress-tls
rules:
- host: api.example.com
http:
paths:
- path: /
backend:
serviceName: service-backend
servicePort: 80
以及在Django应用程序中:
### settings.py
...
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
CORS_ORIGIN_ALLOW_ALL = True
# I also tried setting:
# CORS_ORIGIN_WHITELIST = [
# "https://example.com",
# "https://www.example.com"
# ]
#
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework.authentication.BasicAuthentication'
],
}
...
但是当我从前端应用程序发送请求时,仍然出现错误
Access to XMLHttpRequest at 'https://api.example.com/somepath' from origin 'https://example.com' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
奇怪的是,当我在入口禁用tls时,问题就消失了。如果我使用邮递员或curl
发送请求,则启用了tls,
curl -i -k -H "Authorization: Basic token" https://api.example.com/somepath
我正在收到标头的响应:
HTTP/2 200
server: nginx/1.17.7
date: Mon, 06 Jul 2020 18:58:50 GMT
content-type: application/json
content-length: 9
vary: Accept, Origin
allow: GET, HEAD, OPTIONS
x-frame-options: DENY
x-content-type-options: nosniff
strict-transport-security: max-age=15724800; includeSubDomains
access-control-allow-origin: https://example.com
access-control-allow-credentials: true
access-control-allow-methods: PUT, GET, POST, PATCH, OPTIONS
access-control-allow-headers: DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization,X-CSRFToken
我认为标题在那里,因为入口本身添加了它们(?)
我对整个开发人员世界来说还很陌生,因此,我希望您能从任何角度了解到什么地方或检查什么。对于该问题的大多数答案都建议正确设置我已经尝试过的CORS_ORIGIN_ALLOW_ALL
和/或CORS_ORIGIN_WHITELIST
。
您遇到的问题与配置无关。最重要的是,您尝试跨不同的子域发送请求(这是CORS阻止的)。您需要设置
nginx.ingress.kubernetes.io/cors-allow-origin
至"https://api.example.com"
或从同一个域提供API-例如https://example.com/api
。