KeyCloak 节点 js 从 React/Angular 应用程序访问 API 时出现 CORS 错误

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

我已经在nodejs中添加了keycloak.json文件

{
  "realm": "ABC",
  "auth-server-url": "http://localhost:8080/auth",
  "ssl-required": "external",
  "resource": "AppShellService",
  "credentials": {
    "secret": "MY_SECRET_KEY"
  },
  "confidential-port": 0,
  "enable-cors": true
}

keycloak configuration in App.js

const Keycloak = require("keycloak-connect");
const memoryStore = new session.MemoryStore();
const keycloak = new Keycloak({ store: memoryStore });
app.use(
  keycloak.middleware({
    logout: "/logout",
    admin: "/",
    protected: "/keycloakauth"
  })
);

在 App.js 中我创建了一个 API

app.get("/complain", keycloak.protect(), complaintHandler);

当我从前端应用程序访问“/complain”API 时,出现 CORS 错误

访问 'http://localhost:8080/auth/realms/ABC/protocol/openid-connect/auth?client_id=client&state=8c20862c-d8dc-457b-841a-a08a44e567f6&redirect_uri=http%3A%2F%2Flocalhost来自源“http://localhost:8000”的%3A8000%2Fcomplain%3Fauth_callback%3D1&scope=openid&response_type=code'(从“http://localhost:8000/complain”重定向)已被 CORS 策略阻止:请求的资源上不存在“Access-Control-Allow-Origin”标头。如果不透明响应满足您的需求,请将请求模式设置为“no-cors”以在禁用 CORS 的情况下获取资源。

node.js angular keycloak
1个回答
0
投票

您是否添加了网络来源:http://localhost:8000(末尾没有/)? keycloak 领域仪表板

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