Keycloak 自省端点

问题描述

我正在尝试从我的前端应用程序访问我的 Keycloak 服务器中的内省端点/openid-connect/token/introspect,但出现下一个错误:

Access to fetch at 'http://localhost:8180/auth/realms/backoffice/protocol/openid-connect/token/introspect' from origin 'http://localhost:8080' 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.

使用 Postman、curl 或 Node 应用程序时,此请求工作正常,但从我的前端应用程序使用 fetch 方法显示此错误。我不确定是否可以从浏览器中的前端应用程序查询内省端点,或者是否只能从服务器应用程序查询。

其他端点如:

  • openid-connect/令牌:
  • openid-connect/userinfo:

使用 Postman JS 代码可以正常工作。

Keycloak 配置

我在 Keycloak 的客户已经设置了Web Origins *访问类型机密

Keycloak Config Client

客户端代码

我的前端应用只是 Postman 代码 JS,我使用 node http-server 部署它。

var myHeaders = new Headers();
myHeaders.append("Content-Type","application/x-www-form-urlencoded");

var urlencoded = new URLSearchParams();
urlencoded.append("client_id","my-client");
urlencoded.append("client_secret","my-secret");
urlencoded.append("token","eyJ...oCA");

var requestOptions = {
  method: 'POST',headers: myHeaders,body: urlencoded,redirect: 'follow'
};

fetch("http://localhost:8180/auth/realms/backoffice/protocol/openid-connect/token/introspect",requestOptions)
  .then(response => response.text())
  .then(result => console.log(result))
  .catch(error => console.log('error',error));

标题响应

userinfo 端点中的标头响应带有 Access-Control-Allow-Origin 和 Access-Control-Allow-Credentials,但 自省端点中不存在。 >

enter image description here

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)