Istio 1.5 cors 不起作用 - 对预检请求的响应未通过访问控制检查



当在istio-ingressgateway目标上配置Jwt策略时,Cors preflight请求不起作用。

网关

apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
name: api-gateway
namespace: foo
spec:
selector:
istio: ingressgateway # use istio default controller
servers:
- port:
number: 80
name: http
protocol: HTTP
hosts:
- "api.example.com"
tls:
httpsRedirect: true # sends 301 redirects for http requests
- port:
number: 443
name: https
protocol: HTTPS
tls:
mode: SIMPLE
serverCertificate: /etc/istio/ingressgateway-certs/tls.crt
privateKey: /etc/istio/ingressgateway-certs/tls.key
hosts:
- "api.example.com"

虚拟服务

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: backend-vs
namespace: foo
spec:
hosts:
- "api.example.com"
gateways:
- api-gateway
http:
- match:
- uri:
prefix: /api/v1/info
route:
- destination:
host: backend.foo.svc.cluster.local
corsPolicy:
allowOrigin:
- "https://app.example.com"
allowMethods:
- POST
- GET
- PUT
- DELETE
- PATCH
- OPTIONS
allowHeaders:
- authorization
- content-type
- accept
- origin
- user-agent
allowCredentials: true
maxAge: 300s

安全

apiVersion: "security.istio.io/v1beta1"
kind: "RequestAuthentication"
metadata:
name: "jwt-example"
namespace: foo
spec:
selector:
matchLabels:
app: backend
jwtRules:
- issuer: "http://keycloak.foo/auth/realms/example"
jwksUri: "http://keycloak.foo/auth/realms/example/protocol/openid-connect/certs"
---
apiVersion: security.istio.io/v1beta1
kind: AuthorizationPolicy
metadata:
name: require-jwt-example
namespace: foo
spec:
selector:
matchLabels:
app: backend
action: ALLOW
rules:
- from:
- source:
requestPrincipals: ["http://keycloak.foo/auth/realms/example/http://keycloak.foo/auth/realms/example"]
when:
- key: request.auth.claims[groups]
values: ["group1"]

当我在firefox中测试web应用程序时,它运行良好,但在其他浏览器(如opera、chrome、safari(中,它失败了,并出现以下错误:

Access to XMLHttpRequest at 'https://api.example.com/api/v1/info' from origin 'https://app.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.

让我更深思熟虑的是,在firefox中它运行良好,但在其他浏览器中它无法通过

注意:为了验证istio中的cors策略是否正确,我在istio中禁用了该策略,并在firefox中进行了测试,以了解发生了什么,结果是cors确实出现了问题,但当我在firefoxs中重新运行时,在istio重新启用了cors,请求运行良好。

在进行了分段测试并查看了导致错误的原因后,我发现问题出现在我创建在同一服务端口(backend.example.com(上运行的密钥斗篷网关(keycapture.example.com,https默认为443,http默认为80(时。

我所做的是将密钥斗篷暴露到网关上的另一个端口(ingressgateway(。通过以上和角度应用程序,我停止了cors的问题。

相关内容

最新更新