跨域问题是什么:
不同协议、端口或者路由的情况下尝试访问,比如你新建一个服务运行在http的8081端口上但是尝试访问一个https的31309端口的服务就是跨域问题
表现:
把网页的console调出来,访问路由的时候会有类似这种:
test.html:1 Access to fetch at 'http://192.168.64.20:31309/xxx' from origin 'http://localhost:8081' 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. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled
如何解决:
在外部能通过网关访问到服务的情况下(higress之:让流量通过gateway-优快云博客),设置跨域配置:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: foo-python-ingress
annotations:
# Higress专用CORS配置
higress.io/enable-cors: "true" # 这里不要写成cors-enable,都是血泪,这篇博客就是这个配置导致产生的
higress.io/cors-allow-origin: "http://localhost:8081"
higress.io/cors-allow-methods: "GET,POST,OPTIONS"
higress.io/cors-allow-headers: "DNT,Content-Type,Authorization"
higress.io/cors-allow-credentials: "true"
higress.io/cors-max-age: "1728000"
spec:
ingressClassName: higress
rules:
- host: foo.local
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: foo-python
port:
number: 8080