簡體   English   中英

CORS Issue with Angular and Spring Boot REST API With Keycloak

[英]CORS Issue with Angular and Spring Boot REST API With Keycloak

我已經使用keycloak(jboss/keycloak:12.0.3)構建了 Spring Boot REST API(2.3.2.RELEASE)、Angular(v10)應用程序以進行身份驗證和授權。

Spring 引導應用程序已使用KeycloakWebSecurityConfigurerAdapter進行配置,Angular 前端使用"keycloak-angular": "^8.1.0","keycloak-js": "^12.0.2" ,

目前,我可以通過 keycloak login window 使用 Angular 前端成功登錄應用程序。 But there is a CORS error when trying to do a POST or GET request to Spring Boot REST API using HttpClient in Angular Project.

在此處輸入圖像描述

在此處輸入圖像描述

在此處輸入圖像描述

令牌正確綁定到請求。 這個令牌是有效的,因為我可以通過 Postman 使用相同的令牌調用這個API

在此處輸入圖像描述

我嘗試過的解決方案:

  • 將 keycloak 管理員上的 web 原點設置為 * - 不起作用
  • 在 spring 啟動應用程序中的 application.properties 中設置 keycloak.cors = false - 不工作
  • 在 Spring 安全配置中設置 cors.disable - 不工作
@Override
protected void configure(HttpSecurity http) throws Exception {
  super.configure(http);

  http.cors().disable();
}

There was an error in my configuration for cors, so basically your keycloak setup should be done with spring security as follows, It will resolve any kind of CORS related issues with Spring boot and Keycloak.

  1. Go 到 Keycloak 的管理控制台,並將客戶端的“Web Origins”設置為應用程序 web 前端應用程序的地址(例如:- Z80791B3AE7002CB88C246876D9FAA8FZ)(或只是 *localhost:42)。
  2. 在您的 application.properties 設置keycloak.cors = true

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM