Keycloak:请求的资源上不存在“Access-Control-Allow-Origin”标头

问题描述 投票:0回答:2

环境:

Keycloak:端口 8080 上的 18 运行在8081端口的spring boot应用 --Spring 安全 --Spring 云网关 在端口 4200 上运行的 Angular UI

spring security尝试使用Keycloak进行身份验证时抛出如下错误信息

访问 XMLHttpRequest at 'http://xxx:8080/realms/mna/protocol/openid-connect/auth?response_type=code&client_id=cloud-gateway&scope=openid&state=cxZOIY-QnuDANVYpGfV_aO8uRkZNYHJYhL4pKzIHVPQ%3D&redirect_uri=http://xxx:8081 login/oauth2/code/keycloak&nonce=RSHQXckFMyo8TWyqT1GcFN204lOTgXxmip01JTxhfes'(从'http://xxx:8081/cities'重定向)来自'http://xxx:4200'已被CORS策略阻止:没有'Access-Control-Allow -Origin' header 出现在请求的资源上。

Keycloak Realm 的 Web 源 -> 客户端已配置为“*”,但 CORS 问题仍未解决。

浏览器响应头如下:

Cache-Control: no-store, must-revalidate, max-age=0
Content-Language: en
content-length: 3486
Content-Security-Policy: frame-src 'self'; frame-ancestors 'self'; object-src 'none';
Content-Type: text/html;charset=utf-8
Referrer-Policy: no-referrer

Strict-Transport-Security: max-age=31536000; includeSubDomains
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-Robots-Tag: none
X-XSS-Protection: 1; mode=block
keycloak
2个回答
1
投票

如果你足够仔细地观察错误,你会开始注意到错误实际上并不在 Keycloak 中,而是在你的角度应用程序和 spring boot 之间。

因此,您的 keycloak 设置中的任何更改都不会解决此错误。
在这个 SO 问题中已经有很好的答案,请看一下 - CORS with spring-boot and angularjs not working
本质上,您需要启用您的 spring boot 应用程序以允许来自角度应用程序的请求


-1
投票

@Sudhakar 你有什么解决办法吗,即使我也面临同样的问题

© www.soinside.com 2019 - 2024. All rights reserved.